C# IsNullOrWhiteSpace() 方法
csharpserver side programmingprogramming更新于 2025/9/14 7:52:00
C# 中的 IsNullOrWhiteSpace() 方法用于指示指定字符串是否为空、为空或仅由空格字符组成。
语法
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 = "
";
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

