欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【转载】C#中string.IsNullOrEmpty和string.IsNullOrWhiteSpace区别

程序员文章站 2022-03-20 08:30:02
在C#中判断字段是否为空或者Null的时候,我们一般会使用到string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法,这两个方法在大部分情况下判断的结果是一致的,但相比于string.IsNullOrEmpty方法,string.IsNullOrWhiteSp ......

在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技术小趣屋