[英]How to remove empty lines while preserving the formatting of the richtextbox in c#
[英]How to find number of empty lines in RichTextBox C#?
我用這段代碼找到了所有行的計數:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(richTextBox1.Lines.Length.ToString());
}
我怎樣才能只找到空行的數量?
您可以嘗試使用.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
變量將存儲 integer 值,該值表示給定富文本框中的空行數。
richTextBox1.Lines
返回一個字符串數組,您可以使用.Count()
方法從string[]
即 Lines 獲取空行或System.Linq
行的計數
要檢查給定的行是否為空,我們使用string.IsNullOrEmpty()
function,如果字符串參數為空,則返回true
,否則返回false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.