判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?据说答案是C,不太明白,请问为什么啊?谢谢~~

解决方案 »

  1.   

    我认为是 b 好点 ,String.Empty String类自带的 表示为否空字符串
      

  2.   

    a 和 b 是等同的,因为  String.Empty 就是 ""c 最快,因为它不用做 string.Equal(); 相当于直接取一个常量值看  string 类的源码就知道了
      

  3.   

    string.IsNullOrEmpty(str)
    最优秀,但不一定是最快!
      

  4.   

    上面说得好像还是不很清楚,str.Length==0 也进行了判断啊,感觉还是先取出长度再进行的判断,是不是更要浪费时间?
      

  5.   

    我也很好奇这个问题。感觉str.Length效率总应该要低一些。因为要计算长度嘛,不过应该不是这样。洗耳恭听下面的高手解释
      

  6.   

    怎么不先判断是否为null呢?:P
      

  7.   

    应该将a和b,或a和c结合起来用,str==""与str==null是不同的
      

  8.   

    题目的要求判断str是否为空.
    当str对象未分配内存,既str=null时,这个字符串也算为空.str==""和str=String.Empty都不能检测str=null的情况
      

  9.   

    string.IsNullOrEmpty(str)
    最优秀,但不一定是最快!str.Length==0;
    虽然快,但是str为null的时候会抛异常的
      

  10.   

    string.IsNullOrEmpty(str)VS 2005的代码运行推荐用这个,不信你可以试一下。
      

  11.   

    判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?
    ——————————————————————————————————————-----------------------------------------------------
    string tmp;
    tmp.Length=0;
    无法对属性或索引器“string.Length”赋值 -- 它是只读的
    -----------------------------------------------------推荐使用string.Empty。个人觉得更加更加能够让人理解
      

  12.   

    tmp.Length 也容易理解= =
      

  13.   

    str.Length==0
    效率更快
    1.使用Length来检查一个字符串是否为空。
    计算Length时,只需要检查内部数组的长度,不需要字节比较。而字节比较是比直接计算计算要慢很多的
    2.在来看看string的一个构造函数
    String (Char[], Int32, Int32) 
    将 String 类的新实例初始化为由 Unicode 字符数组、该数组内的起始字符位置和一个长度指示的值。
    即string对象在本身构造时,就已经有一个参数是长度指示,他对外的表现就是Length
    3.我们在来看看String.Empty本质,有一定c++和vb基础的都知道,一个字符串都有一个表示结尾的字节,net环境下也是如此,这个结尾的字节就是String.Empty
    4.使用str==String.Empty要比str==""快速因为String.Empty在内部是个常量综上:
    a、str=="";
    b、str==String.Empty;
    c、str.Length==0;
    c优于b,b优于a
      

  14.   

    str.Length==0
    因为他是str的属性,一般属性都是初使化时就有,所以你 用他是直接就有的,
    不用做任何计算。。如果是方法就不同了哈哈
      

  15.   

    using System;
    public class strLengthTime
    {
        public static void Main()
        {
            string strTest = "";
            string strSub;
            for (int i = 0; i < 1000; i++)
            {
                strSub = i.ToString();
                strTest = strTest + strSub;
            }
            DateTime startTime;
            DateTime endTime;
            TimeSpan timeCost;
            startTime = DateTime.Now;
            for (int m = 0; m < 100000000; m++)
            {
                if (strTest.Length == 0)
                {
                }
            }        endTime = DateTime.Now;
            timeCost = endTime - startTime;
            System.Console.WriteLine("strTest.Length Method Costs:" + timeCost.TotalMilliseconds.ToString());
            startTime = DateTime.Now;
            for (int m = 0; m < 100000000; m++)
            {
                if (strTest == string.Empty)
                {
                }
            }
            endTime = DateTime.Now;
            timeCost = endTime - startTime;
            System.Console.WriteLine("String.Empty Method Costs:" + timeCost.TotalMilliseconds.ToString());
            startTime = DateTime.Now;
            for (int m = 0; m < 100000000; m++)
            {
                if (strTest == "")
                {
                }
            }
            endTime = DateTime.Now;
            timeCost = endTime - startTime;
            System.Console.WriteLine("The Third Method Costs:" + timeCost.TotalMilliseconds.ToString());
            Console.ReadLine();
        }
    }
    事实胜于雄辩
      

  16.   

    to:xiaotupansy(中)
    用stopwatch测时间间隔会准确一些:)
      

  17.   

    学习------------------------
    http://fenglin.xland.cn
    ------------------------
      

  18.   

    上CSDN每天都可以学到很多东西啊。记下了。
      

  19.   

    string.IsNullOrEmpty();
    这个方法是标准方法,实际上也是上面有些人用的方法。这是.NET自已封的方法。我用工具反译了一下,这就是它的代码原型:public static bool IsNullOrEmpty(string value)
    {
          if (value != null)
          {
                return (value.Length == 0);
          }
          return true;
    }
    基本上和大家的做法是一样的,所以说还是建议使用系统自带的,这样也省事,图个方便嘛,对不对?
     
      

  20.   

    1. C# 和 C++ 不一样:不是采用0结尾的。2. 由于原因1,调用Length属性(get_Length方法)时,应该不会通过遍历每个字符串直至尾部来取得。而估计是读取String的private字段:m_stringLength。3. CLR对于字符串常量(注意:常量)采用字符串驻留机制(Microsoft.NET.框架程序设计 12.2.4节):即用一个散列表保存字符串常量。
    比如代码:
    string s1 = "hello world";//在散列表中查找“hello world”,结果没找到,就把“hello world”填到了散列表中,并返回引用给s1
    string s2 = "hello world";//找到了“hello world”,返回它的引用给s2
    所以,上面的s1和s2指向同一个字符串而非两个4. 基于原因3, String类在判断两个相等时,先判断两个字符串是否指向同一个,即这是两个引用(Int32 变量)比较,见下面String类的代码:(op_Equality会调用下面的)
    public static bool Equals(string a, string b)
    {
          if (a == b)
          {
                return true;
          }
          if ((a != null) && (b != null))
          {
                return string.EqualsHelper(a, b);
          }
          return false;
    }
    可见,
    string s1 = ""; 
    string s2 = "hello world";
    bool b1 = s1 == "";//语句3
    bool b2 = s2 == "";//语句4
    同样使用 == "";方式语句3的速度也比语句4快。5.下面我写了一个StringEmply类,代码如下:
    public class StringEmpty    {        public static bool A(string str)        {            return str == "";        }
            public static bool B(string str)        {            return str == string.Empty;        }
            public static bool C(string str)        {            return str.Length == 0;        }    }
    用Dasm反汇编得到下列代码
    .method public hidebysig static bool  A(string str) cil managed{  // 代码大小       12 (0xc)  .maxstack  8  IL_0000:  ldarg.0  IL_0001:  ldstr      ""  IL_0006:  call       bool [mscorlib]System.String::op_Equality(string,                                                                 string)  IL_000b:  ret} // end of method StringEmpty::A
    .method public hidebysig static bool  B(string str) cil managed{  // 代码大小       12 (0xc)  .maxstack  8  IL_0000:  ldarg.0  IL_0001:  ldsfld     string [mscorlib]System.String::Empty  IL_0006:  call       bool [mscorlib]System.String::op_Equality(string,                                                                 string)  IL_000b:  ret} // end of method StringEmpty::B
    .method public hidebysig static bool  C(string str) cil managed{  // 代码大小       10 (0xa)  .maxstack  8  IL_0000:  ldarg.0  IL_0001:  callvirt   instance int32 [mscorlib]System.String::get_Length()  IL_0006:  ldc.i4.0  IL_0007:  ceq  IL_0009:  ret} // end of method StringEmpty::C
    B 较 A 多走一步: string [mscorlib]System.String::Empty的结果一定是""
    综上,个人认为:
    C > A > B
    我对查看内部代码时,还存在一些不能确定的问题。所以请各位仁兄指正:对在那里;错在哪里。以满足大伙的好奇心。谢!
    声明:允许骂娘。
     
      

  21.   

    我靠强了解了。str.Length==0;?