【问题标题】:How to find number of empty lines in RichTextBox C#?如何在 RichTextBox C# 中查找空行数?
【发布时间】:2022-01-07 21:17:51
【问题描述】:

我找到了这段代码的所有行数:

  private void button2_Click(object sender, EventArgs e)
    { 
        MessageBox.Show(richTextBox1.Lines.Length.ToString());
    }

我怎样才能只找到空行的数量?

【问题讨论】:

    标签: c# count richtextbox


    【解决方案1】:

    您可以尝试使用.Count()

    .Count() 迭代给定序列并在谓词返回 true 时增加计数。

    private void button2_Click(object sender, EventArgs e)
    { 
         var emptyLineCount = richTextBox1.Lines.Count(x => string.IsNullOrEmpty(x));
         MessageBox.Show(emptyLineCount);
    }
    

    在给定条件应用.Count() 后,emptyLineCount 变量将存储整数值,该整数值表示给定富文本框中的空行数。


    richTextBox1.Lines 返回一个字符串数组,您可以使用 System.Linq 中的 .Count() 方法获取空或 null 的计数 来自string[] 的行,即行

    为了检查给定的行是否为空,我们使用了string.IsNullOrEmpty()函数,如果字符串参数为空,则返回true,否则返回false

    【讨论】:

    • 成功了,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多