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. 学习迭代器实现C#异步编程——仿async/await(一)

    .NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C ...

  2. 基于JSP的在线考试系统-JavaWeb项目-有源码

    开发工具:Myeclipse/Eclipse + MySQL + Tomcat 系统简介: 网络考试系统主要用于实现高校在线考试,基本功能包括:自动组卷.试卷发布.试卷批阅.试卷成绩统计等.本系统结构 ...

  3. jdk1.6 支持 tls1.2协议 并忽略身份验证

    jdk1.6不支持tls1.2协议,jdk1.8默认支持,比较好的解决方案是升级jdk,但是升级jdk风险极大.不能升级jdk的情况下,可以使用如下方式. 引入依赖 <dependency> ...

  4. C++实现二叉树的相应操作

    1. 二叉树的遍历:先序(递归.非递归),中序(递归.非递归),后序(递归.非递归). #include <iostream> #include <string> #inclu ...

  5. delphi 10.2 ---treeview 基本用法

    unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  6. Linux之du命令的使用

    du的用法 du命令用来查看目录或文件所占用磁盘空间的大小.常用选项组合为:du -sh du常用的选项: -h:以人类可读的方式显示 -a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘 ...

  7. java连接hbase时出现....is accessible from more than one module:

    今天在用java程序连接hbase时,出现错误,The package org.apache.hadoop.hbase is accessible from more than one module: ...

  8. leetcode-31-下一个排列

    本题目在凌应标老师的<算法设计与分析>第八次作业中出现,可供参考. 题目描述: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的 ...

  9. POJ 2192

    #include <iostream> #include <string> #define MAXN 500 using namespace std; bool dp[MAXN ...

  10. SVN服务器端环境搭建步骤

    5.1 安装服务器端程序 yum install -y subversion 5.2 创建并配置版本库 创建版本库目录 mkdir -p /var/svn/repository 在版本库目录下创建具体 ...