【问题标题】:Make only two empty line spaces between each sentence on RichTextBox.在 RichTextBox 上的每个句子之间只留两个空行空格。
【发布时间】:2013-08-20 02:48:47
【问题描述】:

我在 RichTextBox 中有 100 多行。有些行是空的,有些行有一个句子。我想在 RichTextBox 中的每个句子之间只留两个空行空格。我该怎么做?

List<string> rt = new List<string>();
foreach (string line in richTextBox1.Lines)
{
   if (line != "")
   {
       rt.Add(line);
   }
}

richTextBox1.Lines = rt.ToArray();

【问题讨论】:

  • 在rt.Add(line)之后直接调用rt.Add("")?
  • @user2661591 试试我的答案。
  • 它有效。感谢 rene!

标签: c# regex richtextbox


【解决方案1】:

这是您需要的,经过测试并且工作正常(虽然效率不是很高)

List<string> rt = new List<string>();
bool doAdd = true;
foreach (string line in richTextBox1.Lines)
{
    if (doAdd)
    {
        rt.Add(line);
        doAdd = true;
    }
    if (string.IsNullOrEmpty(line))
    {
          doAdd = false;
    }
    else
    {
         if (!doAdd)
           rt.Add(line);
         doAdd = true;
    }
}
richTextBox1.Lines = rt.ToArray();

【讨论】:

    【解决方案2】:

    我无法测试这个,但你当然可以试试这个

            List<string> rt = new List<string>();
            string content = richTextBox.Text;
            foreach (string line in content.Split('\n'))
            {
                if (String.IsNullOrWhiteSpace(line))  //checks if the line is empty
                    continue;
                rt.Add(line);
                rt.Add("\r\n");  //makes a new line
                rt.Add("\r\n");   // makes another new line
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多