以前刚入行的时候判断字符串的时候用

string a="a";
a=="";
a==null;

后来发现了String.IsNullOrEmpty感觉方便了好多,但是后来发现如果字符串的空白String a="  ";IsNullOrEmpty就没法判断了,于是我今天发现了String.IsNullOrWhiteSpace,此方法只在framework4.0以上才能使用,官方的解释是:指示指定的字符串是 null、空还是仅由空白字符组成。

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

             string a = null;
string b = string.Empty;
string c = "";
string d = " ";
Console.WriteLine("a:{0};\r\n b:{1};\r\n c:{2};\r\n d:{3};\r\n", a, b, c, d);
if (string.IsNullOrEmpty(a))
Console.WriteLine("a");
if (string.IsNullOrEmpty(b))
Console.WriteLine("b");
if (string.IsNullOrEmpty(c))
Console.WriteLine("c");
if (string.IsNullOrEmpty(d))
Console.WriteLine("d"); if (string.IsNullOrWhiteSpace(a))
Console.WriteLine("a1");
if (string.IsNullOrWhiteSpace(b))
Console.WriteLine("b1");
if (string.IsNullOrWhiteSpace(c))
Console.WriteLine("c1");
if (string.IsNullOrWhiteSpace(d))
Console.WriteLine("d1");
Console.Read();

执行结果:

由此可见当用IsNullOrEmpty时,d是没有输出来的,但是string.IsNullOrWhiteSpace却可以,如果执意要用前者又要判断空白的话,不妨与Trim组合使用。

String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别的更多相关文章

  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. string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别

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

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

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

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

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

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

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

  7. String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace

    String.IsNullOrEmpty 指示指定的字符串是否为 null 或者 空字符串: 返回值:如果参数为 null 或者 空字符串("" .String.Empty),结果 ...

  8. 转:String.Empty、string=”” 和null的区别

    原文地址:http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html String.Empty是string类的一个静态常量: Str ...

  9. String.Empty、string=”” 和null的区别

    String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...

随机推荐

  1. Android 滑动效果基础篇(三)—— Gallery仿图像集浏览

    Android系统自带一个Gallery浏览图片的应用,通过手指拖动时能够非常流畅的显示图片,用户交互和体验都很好. 本示例就是通过Gallery和自定义的View,模仿实现一个仿Gallery图像集 ...

  2. fedora 安装lamp

    一.安装apache服务器1.1使用yum进行安装:# yum install httpd1.2配置Apache能够随系统启动而启动#chkconfig --levels 235 httpd on1. ...

  3. SQL Server中内连接和外连接的区别

    SQL Server中内连接和外连接的区别 假设一个数据库中有两张表,一张是学生表StudentInfo,一张是班级表ClassInfo,两张表之间用ClassId字段进行关联. 如果用内连接,正常的 ...

  4. C#_Jquery无刷新上传

    昨晚网友有下载了一个jQuery无刷新上传的小功能,他尝试搬至ASP.NET MVC应用程序中去,在上传死活无效果.Insus.NET使用Teamviewer远程桌面,操作一下,果真是有问题.网友是说 ...

  5. linux乱码问题

    命令输入: export LANG=zh_CN.GBK grep 匹配时高亮 先执行:export GREP_OPTIONS='--color=auto'; 后执行:grep 匹配内容 文件; 描述: ...

  6. VBoxGuestAdditions下载地址

    http://dlc.sun.com.edgesuite.net/virtualbox/

  7. 双系统下,Windows如何正确删除Linux系统

    一般电脑装了双系统,特别是Windows加Linux的电脑,不可以在Windows中直接删了linux,因为一般安装linux的时候,grub都写进了mbr,直接删了Windows就进不了了,除非原来 ...

  8. Pretty Poem

    Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are man ...

  9. DOM+Javascript一些实例

    1.内容+遮罩层+悬浮对话框 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  10. nodejs的mysql模块学习(三)数据库连接配置选项详解

    连接选项 当在创建数据连接的时候 第一种大多数人用的方法 let mysql = require('mysql'); let connection = mysql.createConnection({ ...