473,385 Members | 1,587 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,385 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 2305
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>&); };
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.