一、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 | " " | ||
本文介绍了C#中两种常见的字符串判断方法:string.IsNullOrEmpty与string.IsNullOrWhiteSpace。前者用于检查字符串是否为null或string.Empty,而后者则能进一步判断字符串是否包含空白字符。通过这两种方法,可以有效地进行字符串的有效性验证。
601

被折叠的 条评论
为什么被折叠?



