C#中的IsNullOrWhiteSpace()方法
如果输入的字符串只有空白字符或者为 null,则此方法返回 true。
比如,你检查空值——
bool val1 = string.IsNullOrWhiteSpace(null);
由于字符串为“null”,因此上述返回 True。用同样的方式,检查是否存在空白字符。
以下是检查 null 和空白的完整示例。
示例
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP