473,231 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

compare string

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
Jun 27 '08 #1
11 2297
On Tue, 24 Jun 2008 23:59:23 -0700, Tony <jo*****************@telia.com>
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 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
Jun 27 '08 #2
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:
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
Jun 27 '08 #3
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
Jun 27 '08 #4
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
Jun 27 '08 #5
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
Jun 27 '08 #6
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
Jun 27 '08 #7
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]" <sk***@pobox.comskrev i meddelandet
news:d8**********************************@d45g2000 hsc.googlegroups.com...
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
Jun 27 '08 #8
On Jun 25, 9:30*am, "Tony" <johansson.anders...@telia.comwrote:
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
Jun 27 '08 #9
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

Jun 27 '08 #10
Cor Ligthert[MVP] <no************@planet.nlwrote:
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #11
Tonny,

snip
Have I understodd this correct ?
snip

Yes

To explain a little bit more:

Comparing strings does never compare references.

Your string can be endless time in memory, it has just is special kind of
behaviour.

Any little change to a string does create another string, becasuse every
string is immutable.

So comparing a string will never tell if it is in memory the same string,
however who cares, you and me are probably not able to create two strings
who consumes 100 kb of memory which are the same.

Cor

"Tony" <jo*****************@telia.comschreef in bericht
news:eD**************@TK2MSFTNGP06.phx.gbl...
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]" <sk***@pobox.comskrev i meddelandet
news:d8**********************************@d45g2000 hsc.googlegroups.com...
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

Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Drew | last post by:
Hello - I am a converted VB programmer. What I am trying to do it compare two strings in an if statement. The problem is that when I use string.compare it always returns a negative 1. I have...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
0
by: William Stacey [MVP] | last post by:
Trying to figure out Dictionary<> and using CaseInsensitive Comparer<> like I did with normal Hashtable. The Hashtable can take a case insenstive Comparer and a Case insensitive HashCode provider....
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
2
by: Locia | last post by:
How can I compare "if argument"? example: if (leftExpression==RightExpression) After parsing I know the type of RightExpression. I suppone that if RightExpression is wrap into " " is a...
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.