在C#中判断字段是否为空或者Null的时候,我们一般会使用到string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法,这两个方法在大部分情况下判断的结果是一致的,但相比于string.IsNullOrEmpty方法,string.IsNullOrWhiteSpace方法还会对空白字符进行判断,例如一个字符串全是空格等空白字符的情况,在string.IsNullOrEmpty的判断下为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技术小趣屋

【转载】C#中string.IsNullOrEmpty和string.IsNullOrWhiteSpace区别的更多相关文章

  1. C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)

    一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: .string name = value; if (name ...

  2. 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)

    1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...

  3. string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别

    string.IsNullOrEmpty 都知道,这个功能是判断字符串是否为:null或者string.Empty.如果是如"\t"这样的字符就返回false了,为了达到判断过滤这 ...

  4. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()

    转自:http://hi.baidu.com/saclrpqmttbntyq/item/4592fc72c5a19e5c0d0a07eb 由于总用 String.IsNullOrEmpty( s ) ...

  5. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别

    string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“   ” 这样的字符就返回fa ...

  6. String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace

    String.IsNullOrEmpty 指示指定的字符串是否为 null 或者 空字符串: 返回值:如果参数为 null 或者 空字符串("" .String.Empty),结果 ...

  7. 转载JQuery 中empty, remove 和 detach的区别

    转载 http://www.cnblogs.com/lisongy/p/4109420.html .empty()  描述: 从DOM中移除集合中匹配元素的所有子节点. 这个方法不接受任何参数. 这个 ...

  8. 转载 Python中关键字global与nonlocal的区别

    转载自CSDN 雁丘1990, 原文地址: https://blog.csdn.net/xcyansun/article/details/79672634 这篇文章写的很赞, 条理清晰, 分析循序渐进 ...

  9. [转载]C++中 引用&与取地址&的区别

    一个是用来传值的 一个是用来获取首地址的 &(引用)==>出现在变量声明语句中位于变量左边时,表示声明的是引用.     例如: int &rf; // 声明一个int型的引用r ...

随机推荐

  1. 把pdf的内容转化为txt文件

    import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.util.PDFTextStripper; import j ...

  2. RSA前台加密后台解密

    RSA解密时BadPaddingException java rsa 解密报:javax.crypto.BadPaddingException: Decryption error Java安全架构__ ...

  3. SQLServer string_split函数,撕裂函数,撕开函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...

  4. [转]npm安装教程

    原文地址:https://www.cnblogs.com/lgx5/p/10732016.html 一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpa ...

  5. 使用nodejs+http(s)+events+cheerio+iconv-lite爬取2717网站图片数据到本地文件夹

    源代码如下:   //(node:9240) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' ...

  6. Leetcode: Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  7. Qt开发经验小技巧11-20

    获取类的属性 const QMetaObject *metaobject = object->metaObject(); int count = metaobject->propertyC ...

  8. 8、1 周末总结+Mongdb

    都说加一个Id 注解就行了,其实还要加一条数据测试表是否生成 这里我们探讨  数据库是谁,表是谁 数据库在mongo启动的时候就指定了,这个无需我们关心 表根据实体类自动生成, 1.pom.xml & ...

  9. LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

  10. 09 Servlet相关知识点---学习笔记

    1.概念:运行在服务器端的小程序 Servlet就是一个接口,定义了Java类被浏览器访问到(tomcat识别)的规则.将来我们自定义一个类,实现Servlet接口,复写方法. 2.快速入门:(1)创 ...