比较字符串 [英] compare string

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

问题描述

您好!


下面我有两种不同的方法来测试实例tb.Text是否包含

字符串Programmer


那么使用哪一个只是一个品味问题?

或者它可能是一个先于另一个优势。

In我读过他们曾经使用过的第二本书,但我更喜欢第一本书,因为它更清晰。


if(tb.Text =="程序员)

{

......

}


if(tb .Text.CompareTo(程序员)

{

......

}


// Tony

Hello!

Below I have two different ways to test if the instance tb.Text contains the
string "Programmer"

So which one to use is it just a matter of taste ?
Or could it be some advantage to one before the other.
In a book I read they had used the second one but I prefer the first one
because it it clearer.

if (tb.Text == "Programmer")
{
......
}

if (tb.Text.CompareTo("Programmer")
{
......
}

//Tony

推荐答案

2008年6月24日星期二23:59:23 -0700,Tony< jo **** *************@telia.com>

写道:
On Tue, 24 Jun 2008 23:59:23 -0700, Tony <jo*****************@telia.com>
wrote:

下面我有两种不同的方式测试实例tb.Text是否包含



字符串Programmer


那么一个t o使用它只是一个品味的问题?

或者它可能是一个优先于另一个。

在一本书中我读过他们使用过第二个但是我更喜欢第一个

,因为它更清晰。
Below I have two different ways to test if the instance tb.Text contains
the
string "Programmer"

So which one to use is it just a matter of taste ?
Or could it be some advantage to one before the other.
In a book I read they had used the second one but I prefer the first one
because it it clearer.



我绝对不会使用CompareTo()只是为了测试平等。至少
,我会使用Equals()。你没有发布正确的代码

足以表明调用的方式有多尴尬" CompareTo()"对于这个测试,

真的如此。 :)


那就是说,对于字符串,我总是只使用==运算符。它只是看起来对我来说更可读。具有讽刺意味的是,我认为对某些人而言,

相反是正确的。他们会觉得英文单词比印刷符号更清晰。对每一个他自己。 :)


不,最后我不相信有任何明显的优势

。充其量,可能会有一些微妙的表现

的差异,但毫无疑问也不足以保证牺牲

的可读性。


Pete

I would definitely not use "CompareTo()" just to test for equality. At
the very least, I''d use "Equals()". You didn''t post code that was correct
enough to show just how awkward calling "CompareTo()" for this test would
really be. :)

That said, for strings I''d always just use the == operator. It justseems
more readable to me. Ironically, I think that for some people the
opposite would be true. They would feel that an English word is more
clear than a typographical symbol. To each his own. :)

And no, in the end I don''t believe that there''s any significant advantage
of one over the other. At best, there might be some subtle performance
difference, but undoubtedly not one large enough to warrant sacrificing
readability.

Pete


你好!


你提到使用CompareTo真的是一个糟糕的选择而且真的是

尴尬。

是否因为某些特别原因使用CompareTo是一个糟糕的选择



正如我所说,我不是使用它,但当你sais我引用你没有发布代码,

是正确的

足以表明调用有多尴尬" CompareTo()"对于这个测试,

真的如此。 :)"


// Tony


" Peter Duniho" < Np ********* @nnowslpianmk.comskrev i meddelandet

news:op *************** @ petes-computer.local ...

2008年6月24日星期二23:59:23 -0700,Tony< jo ***************** @ telia.com> ;

写道:
Hello!

You mentioned that using the CompareTo is really a bad choice and really
awkward.
Is it for some particularly reason that using the CompareTo is a bad choice
?
As I said I don''t use it but as you sais I quote "You didn''t post code that
was correct
enough to show just how awkward calling "CompareTo()" for this test would
really be. :)"

//Tony

"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...
On Tue, 24 Jun 2008 23:59:23 -0700, Tony <jo*****************@telia.com>
wrote:

下面我有两种不同的方法来测试实例tb.Text是否包含



字符串程序员


那么使用哪一个只是品味问题?

或者它可以吗在一本书之前有一些优势。

在我读过的一本书中他们使用了第二本但我更喜欢第一本

,因为它更清晰。
Below I have two different ways to test if the instance tb.Text contains
the
string "Programmer"

So which one to use is it just a matter of taste ?
Or could it be some advantage to one before the other.
In a book I read they had used the second one but I prefer the first one
because it it clearer.



我绝对不会使用CompareTo()只是为了测试平等。至少
,我会使用Equals()。你没有发布正确的代码

足以表明调用的方式有多尴尬" CompareTo()"对于这个测试,

真的如此。 :)


那就是说,对于字符串,我总是只使用==运算符。它似乎对我来说更具可读性。具有讽刺意味的是,我认为对某些人而言,

相反是正确的。他们会觉得英文单词比印刷符号更清晰。对每一个他自己。 :)


不,最后我不相信有任何明显的优势

。充其量,可能会有一些微妙的表现

的差异,但毫无疑问也不足以保证牺牲

的可读性。


Pete

I would definitely not use "CompareTo()" just to test for equality. At
the very least, I''d use "Equals()". You didn''t post code that was correct
enough to show just how awkward calling "CompareTo()" for this test would
really be. :)

That said, for strings I''d always just use the == operator. It just seems
more readable to me. Ironically, I think that for some people the
opposite would be true. They would feel that an English word is more
clear than a typographical symbol. To each his own. :)

And no, in the end I don''t believe that there''s any significant advantage
of one over the other. At best, there might be some subtle performance
difference, but undoubtedly not one large enough to warrant sacrificing
readability.

Pete


如果实例tb.Text包含字符串Programmer


就个人而言,因为我认为正确的结果是就像清晰度一样,我会支付:b $ b可能用于:


if(tb.Text.Contains(" Programmer")){... }


Marc
if the instance tb.Text contains the string "Programmer"

Personally, since I value the right result as much as clarity, I''d
probably go for:

if(tb.Text.Contains("Programmer")) {...}

Marc


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

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