String.IsNullOrEmpty

指示指定的字符串是否为 null 或者 空字符串;

返回值:如果参数为 null 或者 空字符串("" 、String.Empty),结果为true,否则为false。

等效于以下代码:

result = s == null || s == String.Empty;

String.IsNullOrWhiteSpace

指示指定的字符串是否为 null、空字符串 或者 仅由空字符组成。

返回值:如果参数为 null 、 空字符串("" 、String.Empty) 或者 仅由空字符组成,结果为true,否则为false。

等效于以下代码:

result = String.IsNullOrEmpty(s) || s.Trim().Length == ;

测试代码:

             string s1 = null;
string s2 = string.Empty;
string s3 = "";
string s4 = " ";
string s5 = "\t"; try
{
Console.WriteLine("The length of '{0}' is {1}.", s1, s1.Length);
}
catch (NullReferenceException ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The length of '{0}' is {1}.", s2, s2.Length);
Console.WriteLine("The length of '{0}' is {1}.", s3, s3.Length);
Console.WriteLine("The length of '{0}' is {1}.", s4, s4.Length);
Console.WriteLine("The length of '{0}' is {1}.", s5, s5.Length); Console.WriteLine("-------------------------------------------------------"); Console.WriteLine(string.IsNullOrEmpty(s1));
Console.WriteLine(string.IsNullOrEmpty(s2));
Console.WriteLine(string.IsNullOrEmpty(s3));
Console.WriteLine(string.IsNullOrEmpty(s4));
Console.WriteLine(string.IsNullOrEmpty(s5)); Console.WriteLine("-------------------------------------------------------"); Console.WriteLine(string.IsNullOrWhiteSpace(s1));
Console.WriteLine(string.IsNullOrWhiteSpace(s2));
Console.WriteLine(string.IsNullOrWhiteSpace(s3));
Console.WriteLine(string.IsNullOrWhiteSpace(s4));
Console.WriteLine(string.IsNullOrWhiteSpace(s5));

结果:

未将对象引用设置到对象的实例。
The length of '' is 0.
The length of '' is 0.
The length of '    ' is 4.
The length of ' ' is 1.
-------------------------------------------------------
True
True
True
False
False
-------------------------------------------------------
True
True
True
True
True

相关资料:

https://msdn.microsoft.com/zh-cn/library/system.string.isnullorempty(v=vs.110).aspx

https://msdn.microsoft.com/zh-cn/library/system.string.isnullorwhitespace(v=vs.110).aspx

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. 【转载】C#中string.IsNullOrEmpty和string.IsNullOrWhiteSpace区别

    在C#中判断字段是否为空或者Null的时候,我们一般会使用到string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法,这两个方法在大部分情况下判断的结果是一致的 ...

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

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

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

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

  5. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()

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

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

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

  7. String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别

    以前刚入行的时候判断字符串的时候用 string a="a"; a==""; a==null; 后来发现了String.IsNullOrEmpty感觉方便了好多 ...

  8. 【源码分析】你必须知道的string.IsNullOrEmpty && string.IsNullOrWhiteSpace

    写在前面 之前自信撸码时踩了一次小坑,代码如下: private static void AppServer_NewMessageReceived(WebSocketSession session, ...

  9. 再谈扩展方法,从string.IsNullOrEmpty()说起

    string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...

随机推荐

  1. php foreach 遍历细节探讨

    foreach 也是正常的语法循环结构,可以有 break 和 continue 等操作 遍历过程中值变量传递默认是值传递 输出结果为: 遍历过程中值变量可以认为设定为引用传递:foreach($数组 ...

  2. CentOS7.x安装时的分区方案

    -------------------------------------------------分区方案描述--------------------------------------------- ...

  3. js简单正则表达式验证密码

    包含3种及以上 var reg = new RegExp("^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\ ...

  4. gulp 搭建个人工作流:文件注入、热启动、跨域

    个人比价推崇前后端分离的开发方式,大家伙各司其职,只需通过 API 进行交流,不仅避免了沟通上的成本,更提升了开发效率.而在前端开发工作中,许多需求和问题是相似的,所以我们的开发模式往往是雷同的,是否 ...

  5. Memcached安装教程及使用

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载 Table of contents 安装 使用 在spring中使用 安装 下载下来m ...

  6. POJ1095 Trees Made to Order(JAVA)

    这题用到了卡特兰数,比较麻烦.关于卡特兰数的基本概念百度一下你就知道. 使用卡特兰数对数组元素进行分组之后,需要具体计算一下要求的是第几组的第几个数,然后向下递归. 首先来看利用卡特兰数分组: 从1开 ...

  7. 线索二叉树的理解和实现(Java)

    线索二叉树的基本概念 我们按某种方式对二叉树进行遍历,将二叉树中所有节点排序为一个线性序列,在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结 ...

  8. mysql 与sqlser group by

    mysql select * ,count(1)  from simccbillm18 group by MonthNum; SqlSer select  col1,col2 from table g ...

  9. Eigenface与PCA人脸识别算法实验

    简单的特征脸识别实验 实现特征脸的过程其实就是主成分分析(Principal Component Analysis,PCA)的一个过程.关于PCA的原理问题,它是一种数学降维的方法.是为了简化问题.在 ...

  10. 最小生成树--牛客练习赛43-C

    牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 ​ 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...