【问题标题】:Break on space, textbox (specific condition)打破空间,文本框(特定条件)
【发布时间】:2011-11-23 02:22:13
【问题描述】:

我需要在最接近 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”等字符,因为它们用于文本格式。

【问题讨论】:

    标签: c# asp.net line-breaks


    【解决方案1】:

    虽然可能不是最干净的解决方案。如果您只是依靠 @(或执行正则表达式来检测模式)并将该数字添加到 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);
    

    【讨论】:

      【解决方案2】:
      textBox1.Text.Replace("@A", "").Replace("@B", "")...
      

      【讨论】:

      • 临时替换为不可用的字符。
      • @RedHat 例如,@A 在外部应用程序中为文本赋予新颜色,它不应该被计算在内,但必须保持原样。
      • 好的,用“”替换:Replace("@A", "")
      【解决方案3】:

      你可以试试下面的代码。

      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);
      } 
      

      【讨论】:

      • 嗨。这工作正常,但只有一次。我们可以使它适用于多行吗?谢谢。
      • @el ninho:检查我的第二个答案
      【解决方案4】:
              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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多