IsNullOrWhiteSpace()C#中的方法用于指示指定的字符串是null,空还是仅由空格字符组成。
public static bool IsNullOrWhiteSpace (string val);
上面的参数val是要测试的字符串。现在让我们看一个例子-
现在让我们看一个例子:
using System;
public class Demo {
public static void Main() {
string str1 = null;
string str2 = String.Empty;
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
}
}输出结果
这将产生以下输出-
Is string1 null or whitespace? = True Is string2 null or whitespace? = True
现在让我们来看另一个例子-
using System;
public class Demo {
public static void Main() {
string str1 = "\n";
string str2 = "Tim";
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
}
}输出结果
Is string1 null or whitespace? = True Is string2 null or whitespace? = False