检查空字符串 [英] checking for empty string

查看:96
本文介绍了检查空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你使用哪一个?为什么?


MyString == null || MyString ==""


vs


MyString == null || MyString.Length == 0

which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0

推荐答案

检查长度比检查字符串是否为空更好:)

再见。


-------------------

LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9

Dan Bass写道:
Checking the length is better than checking if the string is empty or not :)

Bye.

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9
"Dan Bass" wrote:
你使用哪一个?为什么?

MyString == null || MyString ==""

vs

MyString == null || MyString.Length == 0
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0



我通常使用:
MyString == null || MyString.Length == 0


与Lutz Roeder的.Net Reflector相比,看来这个

版本相当于MyString == null || MyString.m_stringLength == 0


而另一个版本如果相当于

MyString == null ||
CultureInfo.CurrentCulture.CompareInfo.Compare(MyS tring,new String(""),

CompareOptions.None)== 0


-

真相,

James Curran

[以前的VC ++ MVP]

主页: www.noveltheory.com 工作: www.njtheater.com

博客: www.honestillusion.com 日间工作: www.partsearch.com


Dan Bass < danielbass [at] postmaster [dot] co [dot] uk>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...您使用哪一个?为什么?

MyString == null || MyString ==""

vs

MyString == null || MyString.Length == 0
MyString == null || MyString.Length == 0
Poking around a bit with Lutz Roeder''s .Net Reflector, it seems that this
version is the equivalent of MyString == null || MyString.m_stringLength == 0
while the other version if the equivalent of
MyString == null || CultureInfo.CurrentCulture.CompareInfo.Compare(MyS tring, new String (""),
CompareOptions.None) == 0

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0



<" Dan Bass" < danielbass [at] postmaster [dot] co [dot] uk>>写道:
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
你使用哪一个?为什么?

MyString == null || MyString ==""

vs

MyString == null || MyString.Length == 0
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0




嗯,后者效率稍高,所以你应该使用它,如果

这是一个瓶颈你的应用程序,但实际上它是

的情况,在大多数情况下你认为最具可读性。我个人比较喜欢

,但这真的是一个品味问题。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet

如果回复小组,请不要给我发邮件



Well, the latter is slightly more efficient, so you should use that if
this is a bottleneck in your application, but really it''s a case of
whichever you find most readable, in most cases. I personally prefer
the first, but it''s really a matter of taste.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于检查空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆