已解决
C# 中判空方法 string.IsNullOrEmpty() 和 string.IsNullOrWhiteSpace() 区别
来自网友在路上 165865提问 提问时间:2023-11-02 05:29:27阅读次数: 65
最佳答案 问答题库658位专家为你答疑解惑
在 C#
中,string.IsNullOrEmpty
和 string.IsNullOrWhiteSpace
是用于检查字符串是否为空的两种不同方法。
-
string.IsNullOrEmpty
方法检查字符串是否为null
或空字符串。如果传入的字符串为null
或长度为0
,则返回true
;否则返回false
。string str = ""; // 或者 string str = null; if (string.IsNullOrEmpty(str)) {// 字符串为空或者为null }
-
string.IsNullOrWhiteSpace
方法检查字符串是否为null
、空字符串或者只包含空格。如果传入的字符串为null
、长度为0
或者只包含空格,则返回true
;否则返回false
。string str = " "; // 或者 string str = null; 或者 string str = ""; if (string.IsNullOrWhiteSpace(str)) {// 字符串为空、为null或只包含空格 }
区别在于 string.IsNullOrWhiteSpace
还会将字符串中仅包含空格的情况视为空,而 string.IsNullOrEmpty
仅检查是否为 null
或空字符串,不考虑字符串中只包含空格的情况。
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"C# 中判空方法 string.IsNullOrEmpty() 和 string.IsNullOrWhiteSpace() 区别":http://eshow365.cn/6-29919-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!