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

由于原来一直都没注意到这两个方法,一直使用string.IsNullOrEmpty,当看到string.IsNullOrWhiteSpace时,而且在微软人员开发的项目中经常使用时才注意到,查了一下MSDN,记一下免得以后忘记。

string.IsNullOrEmpty

都知道,这个功能是判断字符串是否为:null或者string.Empty。如果是如"\t"这样的字符就返回false了,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了如下的方法。

string.IsNullOrWhiteSpace

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

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "ABCDE",
                          new String(‘ ‘, 20), "  \t   ",
                          new String(‘\u2000‘, 10) };
      foreach (string value in values)
         Console.WriteLine(String.IsNullOrWhiteSpace(value));
   }
}
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True

以上就是代码执行效果,至于性能就听微软的吧,不过string.IsNullOrEmpty和string.IsNullOrWhiteSpace相比,肯定是前面一个性能更高【没有测试过,如果有哪位测试过的可以留言告诉我哦,谢谢!】,所以还是要选择性使用的。

时间: 2024-08-06 06:50:51

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():判断字符串是否为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()和String.IsNullOrWhiteSpace()

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

String的几种初始化方法的区别

参考了: java中String的两种初始化方法 ? String a; String aa = ""; String aaa = "123"; String b = new String(); String bb = new String(""); String bbb = new String("123"); String c = null; ? System.out.println(aa == bb);????????/

string.split()与re.split()方法区别

re模块的split()方法与字符串的split()方法相似,前者是根据 正则表达式模式 分隔字符串,后者是根据 固定的字符串 分割,因此与后者相比,显著提升了字符分割的能力. 如果分隔符没有使用由特殊符号表示的正则表达式来匹配多个模式,那 re.split()和 string.split()的执行过程是一样的. string.split()与re.split()方法区别,布布扣,bubuko.com

(转载)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)

StringBuilder与string的Replace方法的区别

StringBuilder的Replace方法会更改对象本身的值,例StringBuilder s1="1234";s1.Replace("4","5");s1的值为"1235"; string的Replace方法不会更改对象本身的值,例string s1="1234";string s2=s1.Replace("4","5");s1的值仍然为"1234&q