2011-11-23 41 views
0

我需要在最靠近空間突破texbox 30日的性格,我得到的是非常好的答案:休息空間,文本框(具體情況)

var x = 30; 
if (textBox1.Text.Length > x) 
{ 
    var index = textBox1.Text.Select((c, i) => new {c, i}).TakeWhile(q => q.i < x).Where(q => q.c == ' ').Select(q => q.i).Last(); 
    textBox1.Text = textBox1.Text.Insert(index, Environment.NewLine); 
} 

唯一的問題是,我需要從排除計數字符如「@A」,「@B」,因爲它們用於文本格式。

回答

0

雖然也許不是最乾淨的解決方案。如果你只是在@數(或執行一個正則表達式來檢測模式),並添加數爲x(30),如:

  int paramCount = test.Where(c => c == '@').Count(); 

      var index = test.Select((c, i) => new { c, i }) 
          .TakeWhile(q => q.i < x + paramCount) 
          .Where(q => q.c == ' ') 
          .Select(q => q.i) 
          .Last(); 

編輯

爲了確保您的數只計算前30個字符(不包括「@」),可以提前執行聚合:

  int paramCount = test.Select((c, i) => new { c, i }) 
           .Aggregate(0, (count, s) => s.c == '@' && s.i < x + count ? count + 1 : count); 
0
textBox1.Text.Replace("@A", "").Replace("@B", "")... 
+1

由於特殊字符是問題,您決定將其刪除?很棒...... – Polity

+0

暫時替換爲不可用的字符。 –

+0

@RedHat例如,@A在外部應用程序中給出文本新的顏色,它不應該被計數,但必須保持原樣。 –

0

你可以試試下面的代碼。

string sTemp = textBox1.Text.Substring(0, 30); 
sTemp = sTemp.Replace(" @A ", ""); 
sTemp = sTemp.Replace("@A ", ""); 
sTemp = sTemp.Replace(" @A", ""); 
sTemp = sTemp.Replace("@A", ""); 

sTemp = sTemp.Replace(" @B ", ""); 
sTemp = sTemp.Replace("@B ", ""); 
sTemp = sTemp.Replace(" @B", ""); 
sTemp = sTemp.Replace("@B", ""); 

int numberOfLeak = 30 - sTemp.Length; 
var x = 30 + numberOfLeak; 
if (textBox1.Text.Length > x) 
{ 
    textBox1.Text = textBox1.Text.Insert(x, Environment.NewLine); 
} 
+0

嗨。這工作正常,但只有一次。我們可以讓它適用於多行嗎?謝謝。 –

+0

@el ninho:檢查我的第二個答案 – culithay

0
 string oriText = textBox1.Text;//Original text that you input 
     int charPerLine = 30;//Number of character per line 
     string sKeyword = "@A|@B";//You can add more template here, the separator is "|" 
     string[] arrKeyword = sKeyword.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); 
     ArrayList arrListKeyword = new ArrayList(); 
     for (int i = 0; i < arrKeyword.Length; i++) 
     { 
      arrListKeyword.Add(" " + arrKeyword[i] + " "); 
      arrListKeyword.Add(arrKeyword[i] + " "); 
      arrListKeyword.Add(" " + arrKeyword[i]); 
      arrListKeyword.Add(arrKeyword[i]); 
     } 
     int nextIndex = 0; 
     while (true) 
     { 
      //Check if the sub string after the NewLine has enough length 
      if (charPerLine < oriText.Substring(nextIndex).Length) 
      { 
       string sSubString = oriText.Substring(nextIndex, charPerLine); 
       //Replace all keywords with the blank 
       for (int i = 0; i < arrListKeyword.Count; i++) 
       { 
        sSubString = sSubString.Replace(arrListKeyword[i].ToString(), ""); 
       } 

       int numberOfLeak = charPerLine - sSubString.Length; 
       int newLineIndex = nextIndex + charPerLine + numberOfLeak;//find the index to insert NewLine 

       oriText = oriText.Insert(newLineIndex, Environment.NewLine);//Insert NewLine 
       nextIndex = newLineIndex + Environment.NewLine.Length;//Environment.NewLine cost 2 in length 

      } 
      else 
      { 
       break; 
      } 
     } 

     textBox1.Text = oriText;