PC Review


Reply
Thread Tools Rate Thread

compare string

 
 
Tony
Guest
Posts: n/a
 
      25th Jun 2008
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


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      25th Jun 2008
> 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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jun 2008
On Jun 25, 8:14*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> 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. *


Slightly aside from Tony's original point, but...

There's one other important thing to think about when it comes to
string equality - what *kind* of string equality you want.

If you use a == b or just a.Equals(b) it will use ordinal comparison -
in other words, if the sequence of chars in a isn't exactly the same
as in b, it will return false.
If you want cultural equivalence or anything similar, you need the
overload of Equals which accepts a StringComparison.

See http://msdn.microsoft.com/en-us/library/ms973919.aspx for much
more detail and advice.

Jon
 
Reply With Quote
 
Tony
Guest
Posts: n/a
 
      25th Jun 2008
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" <(E-Mail Removed)> skrev i meddelandet
news(E-Mail Removed)...
On Tue, 24 Jun 2008 23:59:23 -0700, Tony <(E-Mail Removed)>
wrote:

> 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.


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


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      25th Jun 2008
Re my last post - ignore that ;-p we might have been using different
meanings of "contains" - i.e. I'll guess you mean "contains *just* the
string ..."; sorry for any confusion.

Re CompareTo(), this returns an int, not a bool; you'd need to check
== 0, which doesn't make it very obvious what it is doing. Equals
would be much clearer.

Marc
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      25th Jun 2008
> Equals would be much clearer.
I'm not having a good day... I meant "much clearer than CompareTo";
for routine tests I'd just use == and for specific tests I'd use the
static methods such as string.Equals("foo","bar",
StringComparison.InvariantCultureIgnoreCase) - that way I don't need
to worry about whether the first or second arg is null.

Marc
 
Reply With Quote
 
Tony
Guest
Posts: n/a
 
      25th Jun 2008
Hello!

I tested these and found that Programmer was written.
This method Equals is comparing references as default but for this instance
tb which has been cast to TextBox has overriden this I assume in
the same way as == compare strings for equality
This literal string is placed somewhere in memory and a reference is
pointing to it. We call this ref_1
The other object tb is also a referenc to an TextBox object.We call this
ref_2.
If this ref_1 is the same as ref_2 then they are refering to the same
object.
But because Equal for TextBox don't compare references it compare the
value(string)
they are the same.

Have I understodd this correct ?


if (tb.Text.Equals("Programmer"))
{
Console.WriteLine("Programmer");
}

//Tony

"Jon Skeet [C# MVP]" <(E-Mail Removed)> skrev i meddelandet
news:d80b461e-04b3-4157-8d79-(E-Mail Removed)...
On Jun 25, 8:14 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> 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.


Slightly aside from Tony's original point, but...

There's one other important thing to think about when it comes to
string equality - what *kind* of string equality you want.

If you use a == b or just a.Equals(b) it will use ordinal comparison -
in other words, if the sequence of chars in a isn't exactly the same
as in b, it will return false.
If you want cultural equivalence or anything similar, you need the
overload of Equals which accepts a StringComparison.

See http://msdn.microsoft.com/en-us/library/ms973919.aspx for much
more detail and advice.

Jon


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jun 2008
On Jun 25, 9:30*am, "Tony" <johansson.anders...@telia.com> wrote:
> I tested these and found that Programmer was written.
> This method Equals is comparing references as default but for this instance
> tb which has been cast to TextBox has overriden this I assume in
> the same way as == compare strings for equality


No, because you're not comparing the TextBox for equality. You're
asking the TextBox for its Text property, which is just a string.
Nothing cares where the string came from.

> This literal string is placed somewhere in memory and a reference is
> pointing to it. We call this ref_1
> The other object tb is also a referenc to an TextBox object.We call this
> ref_2.
> If this ref_1 is the same as ref_2 then they are refering to the same
> object.
> But because Equal for TextBox don't compare references it compare the
> value(string)
> they are the same.
>
> Have I understodd this correct ?


No. If you'd written:

if (tb.Equals("Programmer"))

then it would have returned false - you'd be comparing a string with a
TextBox. As it is, you're comparing tb.Text and "Programmer", and both
of those expressions are strings.

Jon
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      25th Jun 2008
Jon,

Reading the message from Tony, I thought what is he doing, this is meat for
Jons mouth, we get now a long reply about constants, immutability and more
of those things.

Was is with you, are you a little bit ill, I hope not. I saw noting in your
reply about that.

A property in the textbox.Text can in my idea never be the same as the
constant in the equal method.
Then that should be as well a property.

But you can explain this in my idea favorite of you more complete then me.

:-)

Cor

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jun 2008
Cor Ligthert[MVP] <(E-Mail Removed)> wrote:
> Reading the message from Tony, I thought what is he doing, this is meat for
> Jons mouth, we get now a long reply about constants, immutability and more
> of those things.
>
> Was is with you, are you a little bit ill, I hope not. I saw noting in your
> reply about that.
>
> A property in the textbox.Text can in my idea never be the same as the
> constant in the equal method.
> Then that should be as well a property.
>
> But you can explain this in my idea favorite of you more complete then me.


I don't see anything in Tony's post which would make me talk about
immutability or constants. He'd simply been confused "comparing a
TextBox and a string" with "comparing the Text property of a TextBox,
and a string".

I can't think why you'd expect me to be talking about immutability or
constants.

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String.Compare vs String.CompareOrdinal =?Utf-8?B?Sm9l?= Microsoft ASP .NET 2 9th Mar 2006 07:21 PM
String compare doesn't compare? Ken Soenen Microsoft Excel Programming 1 16th Jan 2006 03:40 PM
string compare. benny Microsoft C# .NET 5 11th Aug 2004 06:47 PM
Compare String question: == or String.Compare? Blue Ball Microsoft ASP .NET 4 9th Mar 2004 01:25 AM
Compare String question: == or String.Compare? Blue Ball Microsoft C# .NET 5 9th Mar 2004 01:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:39 PM.