Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to compare two strings in c#?
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Aug-11 2:08am    
I forgot about culture-dependent comparison! Good point. However the second reference is not satisfactory. Even object's "semantic" and referential equivalence is much more non-trivial and even more non-trivial in strings, if you take into account such delicate thing as interning. (I voted 4.)

So, please see my solution.
--SA
It might be helpful,

How to: Compare Strings[^]

:)
 
Share this answer
 
v2
Try as below code.

For Case Sensitive Comparison.

C#
string string1 = "Something";
string string2 = "Something";

if (string1.Equals(string2,StringComparison.Ordinal))
{
 //Strings are Equal
}
else
{
 //Strings are not Equal
}


For Case Insensitive Comparison.

C#
string string1 = "Something";
string string2 = "Something";

if (string1.Equals(string2,StringComparison.OrdinalIgnoreCase))
{
 //Strings are Equal
}
else
{
 //Strings are not Equal
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Aug-11 2:09am    
There is a lot more to it. Please see the solution by Uma and mine.
--SA
Julius Kililiku 13-Dec-13 12:40pm    
For:-For Case Insensitive Comparison more acturate!
protected bool comparePassword(string pword)
{
pword = "Something";
string string2 = "Something]";

if (pword.Equals(string2, StringComparison.InvariantCulture))
{
return (true);
}
else
{
return (false);
}
}
Julius 13/12/2013
Sergey Alexandrovich Kryukov 13-Dec-13 13:50pm    
Makes no sense! Instead of all of the above, you can write:

return pword.Equals(string2, StringComparison.InvariantCulture);
Isn't this obvious?

Or even this:
return pword == string2;
return pword.ToLower() == string2.ToLower();

—SA
 
Share this answer
 
A "comparison" should mean "equal":

C#
string first = //...
string second = //...
if (first == second)
   //then something


Trivial, isn't it? Now, it's good to know, that 1) string is a reference type, but quite unusual type: is perfectly simulates "value semantics"; 2) for every reference type, there can be two (or three) different equivalences; referential and "semantic"; the referential equivalence is absolute and is returned by the method object.ReferenceEquals while "semantic" equivalence is defined by the virtual instance method object.Equals which can be overridden to change the "semantic" equivalence. It stands for all types, not only classes. Also, in any type one can re-redefine the operators "==" and "!=" (only both of them!) which affect comparison just using this operators, specifically. Mismatch between "==", "!=" and <code>object.Equals</code> is possible and would present a pathological case, so elementary requirements of sanity demand matching result of "semantic" comparison.

See http://msdn.microsoft.com/en-us/library/system.object.aspx[^].

Now, what's so unusual with strings? The CLR conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. So, an instance of a literal string with a particular value only exists once in the system.

There is a method to make sure that the semantic equivalence of the strings would cause referential equivalence. This is called interning. See the static methods System.String.IsInterned and System.String.Inter. See the explanations and a code sample here: http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900