首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-03-29 
三种常用的字符串判空串方法三种常用的字符串判空串方法:   1: bool isEmpty (str.Length 0)   2: b

三种常用的字符串判空串方法
三种常用的字符串判空串方法:
   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);
}

热点排行