Home > database >  How to find number of empty lines in RichTextBox C#?
How to find number of empty lines in RichTextBox C#?

Time:01-08

I found the count of all lines with this code:

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

How can I find the number of empty lines only?

CodePudding user response:

You can try using .Count()

.Count() iterates over given sequence and increment the count if the predicate returns true.

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

After applying .Count() with given condition, emptyLineCount variable will store integer value which denotes number of empty lines in give rich text box.


richTextBox1.Lines returns an array of string and you can use .Count() method from System.Linq to get the count of empty or null lines from string[] i.e. Lines

To check given line is empty or not we used string.IsNullOrEmpty() function, which returns true if the string argument is empty otherwise return false

  •  Tags:  
  • Related