在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. mybatis or的用法

    @Test public void test3(){ CaseSmallListExample caseSmallListExample = new CaseSmallListExample(); c ...

  2. arcgis python 获得硬件id

    import time import wmi, zlib def get_cpu_info(): tmpdict = {} tmpdict["CpuCores"] = 0 c = ...

  3. 玩转@Git三剑客

    扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主

  4. unity2019新建LWRP项目出错:Failed to resolve project template

    原因不详,但是在C盘创建项目的确会出现这个问题,改到D盘或E盘就没这个问题了

  5. LoadRunner之录制你的第一个脚本

    LoadRunner安装完成之后,肯定就迫不及待的想要上手试用了.下面就是讲一下LR脚本录制的流程和基本的设置. 1.先放一张脚本录制以及运行的流程图 2.脚本录制步骤 1)以管理员身份打开LR软件, ...

  6. -bash: iostat: command not found解决办法

    [root@testhost ~]# iostat-bash: iostat: command not found IOSTAT 命令不可用,首先确认sysstat包是否安装,sysstat包中包括i ...

  7. ADB命令使用大法

    ​前言 Android开发调试工具ADB的使用.ADB(Android Debug Bridge)是Android SDK中的一个工具, 使用ADB可以直接操作管理Android模拟器或者真实的And ...

  8. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  9. docker笔记1--了解docker

    1 docker是什么 (1)背景 docker是基于go语言实现的开源容器项目.目标是实现"build ship and run any app,anywhere",通过对应用的 ...

  10. Influx Sql系列教程三:measurement 表

    在influxdb中measurement相当于mysql中的表,可以理解为一条一条记录都是存与measurent中的,一个数据库中可以有多个measurement,一个measurement中可以存 ...