C# 中的 IsNullOrWhiteSpace() 方法
csharpprogrammingserver side programming更新于 2025/7/11 4:22:17
如果输入的字符串仅包含空格或为空,则此方法返回 true。
假设您检查了空值 −
bool val1 = string.IsNullOrWhiteSpace(null);
由于字符串为"空",因此上述代码返回 True。以同样的方式,检查其中是否存在空格。
以下是检查是否为空和空格的完整示例。
示例
using System;
using System.IO;
public class Demo {
public static void Main() {
bool val1 = string.IsNullOrWhiteSpace(null);
bool val2 = string.IsNullOrWhiteSpace(" ");
bool val3 = string.IsNullOrWhiteSpace("5");
Console.WriteLine(val1);
Console.WriteLine(val2);
Console.WriteLine(val3);
}
}
输出
True True False

