String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace
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的更多相关文章
- C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)
一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: .string name = value; if (name ...
- 【转载】C#中string.IsNullOrEmpty和string.IsNullOrWhiteSpace区别
在C#中判断字段是否为空或者Null的时候,我们一般会使用到string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法,这两个方法在大部分情况下判断的结果是一致的 ...
- 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)
1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
string.IsNullOrEmpty 都知道,这个功能是判断字符串是否为:null或者string.Empty.如果是如"\t"这样的字符就返回false了,为了达到判断过滤这 ...
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()
转自:http://hi.baidu.com/saclrpqmttbntyq/item/4592fc72c5a19e5c0d0a07eb 由于总用 String.IsNullOrEmpty( s ) ...
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别
string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“ ” 这样的字符就返回fa ...
- String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别
以前刚入行的时候判断字符串的时候用 string a="a"; a==""; a==null; 后来发现了String.IsNullOrEmpty感觉方便了好多 ...
- 【源码分析】你必须知道的string.IsNullOrEmpty && string.IsNullOrWhiteSpace
写在前面 之前自信撸码时踩了一次小坑,代码如下: private static void AppServer_NewMessageReceived(WebSocketSession session, ...
- 再谈扩展方法,从string.IsNullOrEmpty()说起
string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...
随机推荐
- 逻辑编程入门--clojure.core.logic
此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1. 逻辑编程思维: 逻辑编程(逻辑程序设计)是种编程范型,它设置答案须匹配的规则来解决问题,而非设置步骤来 ...
- 【OCP题库-12c】最新CUUG OCP 071考试题库(71题)
71.(32-18) choose three Which three statements indicate the end of a transaction? (Choose three.) A) ...
- 洛谷P3676 小清新数据结构题(动态点分治+树链剖分)
传送门 感觉这题做下来心态有点崩……$RMQ$求$LCA$没有树剖快我可以理解为是常数太大……然而我明明用了自以为不会退化的点分然而为什么比会退化的点分跑得反而更慢啊啊啊啊~~~ 先膜一波zsy大佬 ...
- BZOJ 1412--狼和羊的故事(最小割)
1412: [ZJOI2009]狼和羊的故事 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3316 Solved: 1664[Submit][St ...
- Android 线刷小白教程
Android 线刷小白教程 再说一遍,绝不使用刷机精灵等软件. 一.概念 安卓系统一般把rom芯片分成7个区,如果再加上内置sd卡这个分区,就是8个: hboot分区----------负责启动. ...
- npm 常用配置
npm config list/ls 显示配置信息npm config list/ls -l 更详细npm -h 显示帮助信息,建议多查看npm -l display full usage info ...
- leetcode-852-山脉数组的峰顶索引
题目描述: 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... ...
- P03-Python装饰器
本文总结自oldboy python教学视频. 一.前言 1.装饰器本质上就是函数,功能是装饰其他函数,为其他函数添加附加功能. 装饰器在装饰函数时必须遵循3个重要的原则: (1)不能修改被装饰的函数 ...
- 性能测试 vs 负载测试 vs 压力测试
在做一些软件测试工作时,常常会被提及性能测试.负载测试.压力测试,这也是在软件测试方面最容易混淆的三个概念.之前和一个测试大牛聊天,他和我说常常面试一些测试人员会问一些这样的问题,大多人认为负载测试等 ...
- 编辑距离 区间dp
题目描述 设A和B是两个字符串.我们要用最少的字符操作次数,将字符串A转换为字符串B.这里所说的字符操作共有三种: 1.删除一个字符: 2.插入一个字符: 3.将一个字符改为另一个字符: !皆为小写字 ...