在c#中判断字段是否为空或者null的时候,我们一般会使用到string.isnullorempty和string.isnullorwhitespace方法,这两个方法在大部分情况下判断的结果是一致的,但相比于string.isnullorempty方法,string.isnullorwhitespace方法还会对空白字符进行判断,例如一个字符串全是空格等空白字符的情况,在string.isnullorwhitespace的判断下为false,而string.isnullorwhitespace方法判断则为true。

举例如下:

 string stringd = ”   “;//空白字符串

var resultd1 = string.isnullorempty(stringd);
 var resultd2= string.isnullorwhitespace(stringd);

上述语句的结果resultd1=false,resultd2=true。string.isnullorwhitespace方法认定为这种空白字符也是符合规则的,因此返回true。

 

备注:原文转载自博主个人站it技术小趣屋,原文链接为c#中string.isnullorempty和string.isnullorwhitespace区别_it技术小趣屋。