今天我们一起来探讨C#中常用的字符串判空IsNullOrWhiteSpace的几种方法。该方法用于检查字符串是否为null、空字符串("")或只包含空白字符。如果字符串为null、长度为判空或只包含空白字符(例如空格、制表符、换行符),返回true;否则返回false。与IsNullOrEmpty不同,IsNullOrWhiteSpace会考虑字符串中的空白字符。
判断字符串为空有好几种方法:
方法一: 代码如下:
static void Main(string[] args)
{
string str = "";
if (str == "")
{
Console.WriteLine("a is empty"); ;
}
Console.ReadKey();
}
运行结果:a is empty