三种常用的字符串判空串方法

shenen 2007-10-23 02:36:17
三种常用的字符串判空串方法:

   1: bool isEmpty = (str.Length == 0);
   2: bool isEmpty = (str == String.Empty);
   3: bool isEmpty = (str == "");

  哪种方法最快?
...全文
395 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京的雾霾天 2007-10-26
  • 打赏
  • 举报
回复
觉得使用如下就可以了:
bool b = string.IsNullOrEmpty(string);
q359600900 2007-10-26
  • 打赏
  • 举报
回复
第一种,接分
yfcomeon 2007-10-26
  • 打赏
  • 举报
回复
第一种最快,第二种与第三种是效率是一样的
changkimkim 2007-10-26
  • 打赏
  • 举报
回复
第一种
icefeiji 2007-10-26
  • 打赏
  • 举报
回复
当然第一种~
shinaterry 2007-10-26
  • 打赏
  • 举报
回复
具体您可以测试一下就知道...

我也是测试过才得出结论的...

之所以整数判断最快,是因为没有经过实例化等复杂的过程。
xiaotupansy 2007-10-23
  • 打赏
  • 举报
回复
这个问题问了好多次了
做个试验最简单了

新建个控制台应用程序,代码如下

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();
}
}

自己看看结果就知道了
shenen 2007-10-23
  • 打赏
  • 举报
回复
to shinaterry
请问如何知道第一种方法最快?
shenen 2007-10-23
  • 打赏
  • 举报
回复
第一种:
.method private hidebysig static void Main(string[] args) cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
.entrypoint
.maxstack 2
.locals init (
[0] string str)
L_0000: ldstr ""
L_0005: stloc.0
L_0006: ldloc.0
L_0007: callvirt instance int32 [mscorlib]System.String::get_Length()
L_000c: pop
L_000d: ret
}

第二种
.method private hidebysig static void Main(string[] args) cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
.entrypoint
.maxstack 2
.locals init (
[0] string str)
L_0000: ldstr ""
L_0005: stloc.0
L_0006: ldloc.0
L_0007: ldstr ""
L_000c: call bool [mscorlib]System.String::op_Equality(string, string)
L_0011: pop
L_0012: ret
}

第三种
.method private hidebysig static void Main(string[] args) cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
.entrypoint
.maxstack 2
.locals init (
[0] string str)
L_0000: ldstr ""
L_0005: stloc.0
L_0006: ldloc.0
L_0007: ldsfld string [mscorlib]System.String::Empty
L_000c: call bool [mscorlib]System.String::op_Equality(string, string)
L_0011: pop
L_0012: ret
}
shinaterry 2007-10-23
  • 打赏
  • 举报
回复
第二与第三种根本就是一回事...
shinaterry 2007-10-23
  • 打赏
  • 举报
回复
第一种是最快的...

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧