473,402 Members | 2,072 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,402 software developers and data experts.

String Comparison

Hello,
In comparing strings, is it just as well to use...

string1 == string2

or is it better for any reason to use the String.Compare of String.Equals?

Thanks
Nov 17 '05 #1
4 16024
Randy,

The == operator is defined in terms of String.Equals:

http://msdn.microsoft.com/library/de...alitytopic.asp

If you just need to test whether two strings are equal or not, it's simpler
and clearer to use == than Compare().

Compare() is to be used when you need to check whether a string is "less
than", "equal" or "greater than" other following certain sort criteria for
characters:

http://msdn.microsoft.com/library/de...classtopic.asp

Regards - Octavio
"Randy" <te**@temp.com> escribió en el mensaje
news:ex**************@TK2MSFTNGP14.phx.gbl...
Hello,
In comparing strings, is it just as well to use...

string1 == string2

or is it better for any reason to use the String.Compare of String.Equals?

Thanks

Nov 17 '05 #2

1. if (string1 ==string2 ) will perfrm a case-sensitive test for equality
between two strings.

2. string.Compare(string1,string2) will perform case-insensitive test
between two strings.
Example int AA = string.Compare("man","man") will return 0 which means they
are equal

Whereas int AA =string.compare("man","MAN") will return a negative number
which mean they are different.

Hope this helps
Yonas
"Randy" <te**@temp.com> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
Hello,
In comparing strings, is it just as well to use...

string1 == string2

or is it better for any reason to use the String.Compare of String.Equals?

Thanks

Nov 17 '05 #3
Yonas Hagos <y_*****@hotmail.com> wrote:
1. if (string1 ==string2 ) will perfrm a case-sensitive test for equality
between two strings.
Yes.
2. string.Compare(string1,string2) will perform case-insensitive test
between two strings.
No!
Example int AA = string.Compare("man","man") will return 0 which means they
are equal

Whereas int AA =string.compare("man","MAN") will return a negative number
which mean they are different.


That goes against what you said - you said that string.Compare performs
a case-*insensitive* test, but "man" and "MAN" differ only in case. If
string.Compare were case-insensitive, it would return 0.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
"Randy" <te**@temp.com> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
Hello,
In comparing strings, is it just as well to use...

string1 == string2

or is it better for any reason to use the String.Compare of String.Equals?


Assuming you want a case-sensitive comparison :

string1 == string2 is ok, as long as both sides are strings
(and not objects) at compile time.

eg.
==
string s1 = "abc";
string s2 = s1.ToLower();

if (object.ReferenceEquals(s1, s2))
MessageBox.Show("string references equal");
else
MessageBox.Show("string references not equal"); // A

if (s1 == s2)
MessageBox.Show("(s1 == s2) is true"); // B
else
MessageBox.Show("(s1 == s2) is false");

object o1 = s1;
object o2 = s2;

if (o1 == o2)
MessageBox.Show("(o1 == o2) is true");
else
MessageBox.Show("(o1 == o2) is false"); // C

if (String.Equals(o1, o2))
MessageBox.Show("(String.Equals) is true"); // D
else
MessageBox.Show("(String.Equals) is false");
==
When run, the code above outputs
(at least on my PC) the lines marked at
A, B, C and D.
Watch out for tests like (o1 == o2),
since a reference comparison is performed.
When both sides are objects, IMO, it is
safer to use one of the String.Equals
or object.Equals variants.

HTH,
Stephen

Nov 17 '05 #5

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

Similar topics

10
by: David Graham | last post by:
Hi I have been busy going through the last weeks postings in an attempt to absorb javascript syntax (I guess it's not possible to just absorb this stuff in a passive way - I'm getting way out of...
2
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
8
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
5
by: MaSTeR | last post by:
Can anyone provide a practical short example of why in C# I shouldn't compare two strings with == ? If I write this in JAVA String string1 = "Widget"; if (string1 == "Widget") ...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
4
by: Jim Langston | last post by:
Is there any builtin lowercase std::string compare? Right now I'm doing this: if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 ) AmmoType = Item_Ammo_GunBullet; Is there anything the...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.