读书人

三种常用的字符串判空串方法解决思路

发布时间: 2012-03-29 12:53:13 作者: rapoo

三种常用的字符串判空串方法
三种常用的字符串判空串方法:
   1: bool isEmpty = (str.Length == 0);
   2: bool isEmpty = (str == String.Empty);
   3: bool isEmpty = (str == "");
  哪种方法最快?
   1. 1
   2. 2
   3. 3


[解决办法]
static void Main(string[] args)
{
string str = null;
DateTime dt1 = DateTime.Now;
Console.WriteLine("start:"+dt1);
for (int i = 0; i < int.MaxValue; i++)
{
if (str == "")//改成str.Length == 0试试,快多少!!!
continue;
}
DateTime dt2 = DateTime.Now;
Console.WriteLine("end:"+dt2);
TimeSpan ts = dt2 - dt1;
Console.WriteLine("cost:"+ts.TotalSeconds);
}

读书人网 >C#

热点推荐