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. 字节码执行方式--解释执行和JIT

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.两种执行方式: 解释执行(运行期解释字节码并执行) 强制使用该模式:-Xint 编译为机器码执行(将字 ...

  2. console使用技巧

    http://heikezhi.com/yuanyi/10%E4%B8%AAchrome%20console%E5%AE%9E%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7 ...

  3. Android : M 与 N 的权限管理

    从 M 开始,permission 分为 Normal permission Runtime permission Normal permission 直接在 manifest 里声明就可以用了. a ...

  4. linux 使用内存作为 /tmp 临时文件夹

    方法一:cat /etc/fstabtmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 方法二:mount tmpfs /tmp -t tmpfs -o s ...

  5. Unity下的开发框架--适应web和微端游戏异步资源请求的框架

    一.   内容简介: 1.   框架对Web与微端游戏特性的支持: Web和微端游戏最重要的特性是,资源是持续从服务器上即时下载下来的.而保证体验流畅的关键就是保证资源下载分散到持续的体验过程中,并保 ...

  6. linux网络流量实时监控工具之iptraf 【个人比较喜欢用的流量监控软件】

    linux网络流量实时监控工具之iptraf IPTraf是一个网络监控工具,功能比nload更强大,可以监控所有的流量,IP流量,按协议分的流量,还可以设置过滤器等,如下图 对监控网络来说,这个更适 ...

  7. sql server中的 trimtrailingblanks

    使用sp_help 查出 发现有个这个属性, 如何修改呢? SET ANSI_PADDING ONAlter Table Sys_users_History Alter   column PveSit ...

  8. enumerate 和 dict.items()

    对 list 遍历 a_list = [1,2,3] for index,iterm in enumerate(a_list): print(index,iterm) 对 dict 遍历 dict = ...

  9. spring利用注解方式实现Java读取properties属性值

    1. 建立properties文件:我在resource下面建立一个config文件夹,config文件夹里面有mytest.properties文件,文件内容如下: sam.username=sam ...

  10. 安装 luajit && 给 luajit 安装 cjson

    安装 luajit 步骤: 从官网 http://luajit.org/download.html下载  LuaJIT-2.0.5.tar.gz 解压 tar -xzf LuaJIT-2.0.5.ta ...