有没有办法在c#中检查字符串是null还是空白("")?
目前我必须先检查两个条件,null然后检查空白值
if(val == "" || val == null)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用String.IsNullOrEmpty()检查null或没有数据的字符串引用的方法:
if(String.IsNullOrEmpty(val))
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
还有一种方法String.IsNullOrWhitespace(),其指示指定的字符串是否是空,空,或仅包含空白字符.
if(String.IsNullOrWhitespace(val))
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
以上是以下代码的快捷方式:
if(String.IsNullOrEmpty(val) || val.Trim().Length == 0)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |