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