簡體   English   中英

檢查多行文本框是否只有換行符

[英]Check if multiline textbox has only newline characters

使用.Net 2.0,我如何檢查多行文本框中是否只有換行符?

完全沒有文字,只有\\ n,\\ r等。

if (Regex.IsMatch(@"^[\r\n]+$", text))

您可以使用String.IsNullOrWhiteSpace方法。

if(!string.IsNullOrWhiteSpace(TextBox1.Text))
{

}

這是IsNullOrWhiteSpace的來源-

public static bool IsNullOrWhiteSpace(string value)
{
    if (value == null)
        return true;
    for (int index = 0; index < value.Length; ++index)
    {
        if (!char.IsWhiteSpace(value[index]))
            return false;
    }
    return true;
}

您是否嘗試過用任何方法替換Environment.NewLine

例如,

Dim tbLen as Integer = tb.Text.Length() 'returns 2 for 1 return character

Dim tbLenFiltered As Integer = tb.Text.Replace(Environment.NewLine, String.Empty).Length() 'returns 0 for 1 return character

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM