社区
C#
帖子详情
三种常用的字符串判空串方法
shenen
2007-10-23 02:36:17
三种常用的字符串判空串方法:
1: bool isEmpty = (str.Length == 0);
2: bool isEmpty = (str == String.Empty);
3: bool isEmpty = (str == "");
哪种方法最快?
...全文
402
11
打赏
收藏
三种常用的字符串判空串方法
三种常用的字符串判空串方法: 1: bool isEmpty = (str.Length == 0); 2: bool isEmpty = (str == String.Empty); 3: bool isEmpty = (str == ""); 哪种方法最快?
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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. 深入内部机制:要探讨这
三种
方法
的内部...
sql server
字符串
非空
判
断实现
方法
本文将深入探讨如何在SQL Server中实现
字符串
非空
判
断,并提供一些实用的
方法
。 1. **基础非空
判
断** 在SQL Server中,你可以使用`IS NULL`或`IS NOT NULL`来检查一个字段是否为空。例如,如果你有一个名为`...
c语言 数据结构实现之
字符串
字符串
是一种基本的数据结构,广泛应用于计算机科学和信息技术领域。下面将详细介绍C语言数据结构实现之
字符串
的相关知识点。 一、
字符串
数据结构的定义 在C语言中,
字符串
可以定义为一个字符数组,使用typedef ...
数据结构:第四章 串.ppt
4. 串
判
空:
判
断一个串是否为
空串
。 5. 串比较:比较两个串的大小。 6. 求串长:返回一个串的长度。 7. 串联接:将两个串连接成一个新的串。 8. 求子串:从一个串中提取一个子串。 四、串的模式匹配算法 串的模式...
串的基本操作及其应用[收集].pdf
8. 取子串:将串 s 中从起始位置 pos 开始长度 len 的
字符串
放入另一个串 t,可以使用SubString(HString &S,int pos,int len,HString &T)函数来实现。 9. 串删除:删除串 s 中的子串 t,可以使用Status StrDelete...
C#
111,098
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章