String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()

string.IsNullOrEmpty()

这个是判断字符串是否为:null或者string.Empty或者“”,
但不包含空格 。如果是如"\t"或者“   ” 这样的字符就返回false了,它将会把空格的字符串返回为false,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了IsNullOrWhiteSpace()方法。

string.IsNullOrWhiteSpace()

这个是判断所有空白字符包括空格,功能相当于string.IsNullOrEmpty和str.Trim().Length>0 的总和,即会把空格的字符串返回为true,他将字符串给Char.IsWhiteSpace为ture的任何字符都将是正确的。根据MSDN的说明,这个方法会比调用上述两个方法的性能更高而且简洁,所以在判断这个功能时,推荐使用。

时间: 2024-07-30 23:55:12

String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的相关文章

C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)

一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: 1.string name = value; if (name == null) { name = string.Empty; } 2.使用三元操作符(? :)对上面对吗进行优化: string name = value == null ? string.Empty : value; 上面的两种方式 的代码不够简洁,?? 操作符来进行进一步优化,?? 操作符意

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

由于原来一直都没注意到这两个方法,一直使用string.IsNullOrEmpty,当看到string.IsNullOrWhiteSpace时,而且在微软人员开发的项目中经常使用时才注意到,查了一下MSDN,记一下免得以后忘记. string.IsNullOrEmpty 都知道,这个功能是判断字符串是否为:null或者string.Empty.如果是如"\t"这样的字符就返回false了,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产

转载:string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别

string.IsNullOrEmpty():判断字符串是否为null或者为string.Empty,如果是"\t"这样的字符就返回false,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零 string.IsNullOrWhiteSpace():判断所有空白字符,相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char.IsWhiteSpace为ture的任何字符都将是正确的(推荐使用)

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

string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“   ” 这样的字符就返回false了,它将会把空格的字符串返回为false,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了IsNullOrWhiteSpace()方法. string.IsNullOrWhiteSpace 这个是判断所有空白字符包括空格,功能相当于string.Is

(转载)String.IsNullorEmpty()方法的使用

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace StringIsNull{    public partial class _Default : System.Web.UI.Page    {        protected void Page_Load(o

null 与 string.IsNullOrEmpty 区别

!= null 就是不为null!string.IsNullOrEmpty  不是null且不是""(string.Empty) -----------Response: != null 就是不为null!string.IsNullOrEmpty  不是null且不是""(string.Empty)

【转载】String.Empty、string=”” 和null的区别

String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: ? 1 2 3 4 5 6 7 8 9 10 11 12 public static readonly string Empty; //这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢? //String的构造函数 static String(){     Empty = "";

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

String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 11 12 public static readonly string Empty; //这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢? //String的构造函数 static String(){     Empty = "";//

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

原文地址:http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: public static readonly string Empty; //这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢? //String的构造函