社区
C#
帖子详情
三种常用的字符串判空串方法
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
打赏
收藏
三种常用的字符串判空串方法
三种常用的字符串判空串方法: 1: bool isEmpty = (str.Length == 0); 2: bool isEmpty = (str == String.Empty); 3: bool isEmpty = (str == ""); 哪种方法最快?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
第一种是最快的...
C#
判
断
字符串
为空的几种办法
1.
三种
常用
的
字符串
判
空串
方法
:Length法:bool isEmpty = (str.Length == 0);Empty法:bool isEmpty = (str == String.Empty);General法:bool isEmpty = (str == “”);2. 深入内部机制:要探讨这
三种
方法
的内部机制,我们得首先看看.NET是怎样实现的,也就是要看看.NET的源代码!然而,我们哪里找这些源代码呢?我们同样有
三种
方法
:Rotor法:一个不错的选择就是微软的Rotor,这是微软的一个源代码共享项目。Mono法:另一个不错的选择当然就是真正的开源项目Mono啦!Reflector
雅虎公司C#笔试题 考得很全面!
Question 49. (单选)
三种
常用
的
字符串
判
空串
方法
: 1: bool isEmpty = (str.Length == 0); 2: bool isEmpty = (str == String.Empty); 3: bool isEmpty = (str == ""); 哪种
方法
最快?
【转】
三种
常用
的
字符串
判
空串
方法
1.
三种
常用
的
字符串
判
空串
方法
:Length法:boolisEmpty=(str.Length==0);Empty法:boolisEmpty=(str==String.Empty);General法:boolisEmpty=(str==""); 2.深入内部机制:要探讨这
三种
方法
的内部机制,我们得首先看看.NET是怎样实现的,也就是要看看.NET的...
三种
常用
的
字符串
判
空串
方法
执行速度比较
三种
常用
的
字符串
判
空串
方法
: 1: bool isEmpty = (str.Length == 0); 2: bool isEmpty = (str == String.Empty); 3: bool isEmpty = (str == ""); 哪种
方法
最快? 以下是测试代码: using System; public class strLengt...
java
判
断
字符串
是否为空,如何
判
断
字符串
是否为
空串
?
0. 缘起:本文写作缘起于阮的讨论——《FxCop告诉我,检查一个
字符串
是否为空要用string.Length。》。其实用过FxCop的人都知道它会建议你使用String.Length属性来
判
断
字符串
是否为
空串
,但你又是否明白其中的缘由呢?今天有点闲,特意写下这篇文章,希望有点帮助。1.
三种
常用
的
字符串
判
空串
方法
:Length法:bool isEmpty = (str.Length == 0);...
C#
110,502
社区成员
642,567
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章