一、string.IsNullOrEmpty
这个是判断是否为空null或者string.Empty,如果是" "这样的字符就返回false,为了达到判断过滤这些功能,就需要使用Trim()和Length属性的帮助,判断长度是否为零。
即:string.IsNullOrEmpty(str) || str.Trim().Length == 0
二、string.IsNullOrWhiteSpace
1、这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,
2、string.IsNullOrWhiteSpace就是可以判断" "里面仍然是空的字符串。
三、两者的判断功能
string.IsNullOrEmpty | string.IsNullOrWhiteSpace | ||
1 | null | 1 | null |
2 | string.Empty | 2 | string.Empty |
3 | "" | 3 | "" |
4 | " " |