在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. go 练习:HTTP 处理

    这篇文章只是联系go指南时的笔记吧. package main import ( "fmt" "log" "net/http" ) type ...

  2. 团队作业-Beta冲刺(2/4)

    队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...

  3. Cookie和Session原理

    由于HTTP是无状态的协议,客户程序每次都去web页面,都打开到web服务器的单独的连接,并且不维护客户的上下文信息.如果需要维护上下文信息,比如用户登录系统后,每次都能够知道操作的是此登录用户,而不 ...

  4. Python 拼接字符串的几种方式

    在学习Python(3x)的过程中,在拼接字符串的时候遇到了些问题,所以抽点时间整理一下Python 拼接字符串的几种方式. 方式1,使用加号(+)连接,使用加号连接各个变量或者元素必须是字符串类型( ...

  5. pip3的国内安装源

    1,清华源 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualen 2,豆瓣源 pip3 install -i https ...

  6. 有无关键字new的区别

    function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } const ...

  7. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

  8. 【转】SOA架构和微服务架构的区别

    SOA架构和微服务架构的区别 https://blog.csdn.net/zpoison/article/details/80729052

  9. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  10. pytest+allure测试框架搭建

    https://blog.csdn.net/wust_lh/article/details/86685912 https://www.jianshu.com/p/9673b2aeb0d3 定制化展示数 ...