Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
please can anyone advice me what is the best way to compare strings in c#?

Thanks all.
Posted
Comments
BobJanova 18-May-12 11:34am    
Define 'compare'.
Jit Das 18-May-12 11:43am    
https://www.google.com.au/search?sugexp=chrome,mod=14&sourceid=chrome&ie=UTF-8&q=define%3Acompare
Sergey Alexandrovich Kryukov 18-May-12 11:54am    
No! Define it.
--SA
BobJanova 18-May-12 13:09pm    
Very funny, but compare can mean 'are they equal', 'is one larger than the other in sort order', and various other domain specific things.
[no name] 18-May-12 11:36am    
Define "best"

It is recommended that the Equals method be used, either the static string.Equals or the Equals extension method. See http://msdn.microsoft.com/en-us/library/cc165449.aspx[^]
 
Share this answer
 
Comments
Jit Das 18-May-12 11:42am    
Thank you very much.
VJ Reddy 18-May-12 11:46am    
Good answer. 5!
Sergey Alexandrovich Kryukov 18-May-12 11:56am    
The problem is that OP sarcastically denied the definition. "Comparison" might also mean "<=", etc.
Why using Equals of "==" does the job? Also, referential equality is often important.
Please see my answer.
--SA
Shahin Khorshidnia 18-May-12 12:12pm    
Yes Clifford , I like String.Equals(...). It also has ability to use OrdinalIgnoreCase. (My +5)
Sergey Alexandrovich Kryukov 18-May-12 12:19pm    
Yes, case-sensitivity is a good point. Another one is culture-dependent comparison. I'll add it to my answer.
--SA
I think if you want to compare for equality then the Equals instance method http://msdn.microsoft.com/en-us/library/c64xh8f9[^] of string may be better as it also gives options to account for Culture and Ignorecase as given here http://msdn.microsoft.com/en-us/library/system.stringcomparison[^]

If you want to compare to decide the order in which the two strings are to be placed and want to have options of Culture and IgnoreCase etc. then the Compare static method of string, http://msdn.microsoft.com/en-us/library/cc190529[^] is better

as the CompareTo instance method of string, http://msdn.microsoft.com/en-us/library/35f0x18w[^] does not provide those options.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-May-12 12:05pm    
The problem is that OP sarcastically denied the definition. "Comparison" might also mean "<=" (technically speacking, "string.CompareTo", etc.
Besides, equality method call is not needed. "==" does semantic and object.ReferenceEquals -- referential comparison.
Please see my answer.
--SA
VJ Reddy 18-May-12 12:10pm    
That's true. Hence, in my answer first I have written about equals and then about compare also.
I have seen your answer and your points are well placed.
Thank you.
member60 19-May-12 0:28am    
my 5!
VJ Reddy 19-May-12 12:25pm    
Thank you, member60 :)
The "best" (the term is used ironically) method is "=="; it makes semantic comparison. It is not redundant and is more readable. And System.Object.ReferenceEquals gives you referential comparison. By the way, you can define your own equality (this is often needed) for your type, and in this way using referential equality is the must, because otherwise you would have stack overflow (if you define Equals or "==" and use it in the definition, usually for comparison with null; do you need to explain why?). The semantic of ">" or "<" can be got using System.String.CompareTo.

Please see:
http://msdn.microsoft.com/en-us/library/system.string.aspx[^].

Choose those methods which fit your purpose.

[EDIT]

I almost forgot: semantic string comparison may be case-sensitive or not and depend on culture. I mean, in one culture two technically different strings may be considered equal, or one less then another, but the result could be different in another culture. See all the System.String.Compare method, see the reference above.

—SA
 
Share this answer
 
v2
Comments
VJ Reddy 18-May-12 12:08pm    
Good points. 5!
Sergey Alexandrovich Kryukov 18-May-12 12:08pm    
Thank you, VJ.
--SA
Jit Das 18-May-12 12:19pm    
The link provided by Clifford nice and better.
Sergey Alexandrovich Kryukov 18-May-12 12:25pm    
A good link; but who cares? What is important is that you understand what you did not understand before. Do you finally see that your escapade against "define it" was inappropriate?
Good luck,
--SA
member60 19-May-12 0:30am    
my 5!

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