Concretely what is the difference between
String.Compare(String, String, StringComparison)
andString.Compare(String, String, CultureInfo, CompareOptions)
I feel like that the second one offers more options (comparison using any culture instead of only the current one or invariant one, ignore special characters, ignore the width of katakanas (!!) etc...) than the first one. Both have been introduced it in .NET 2.0 so I guess it can't be a question of backward compatibility.
So what's the difference and when should I use the first one and when should I use the second one?
I had a look at this post and this article, but I think they're dealing with a slightly different matters.
Examples. The right way of comparing String in Java is to either use equals(), equalsIgnoreCase(), or compareTo() method. You should use equals() method to check if two String contains exactly same characters in same order. It returns true if two String are equal or false if unequal.
strcmp is used to compare two different C strings. When the strings passed to strcmp contains exactly same characters in every index and have exactly same length, it returns 0. For example, i will be 0 in the following code: char str1[] = "Look Here"; char str2[] = "Look Here"; int i = strcmp(str1, str2);
The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.
Your answer is in the remarks for the second overload.
http://msdn.microsoft.com/en-us/library/cc190529.aspx
"The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it."
The other overload just uses the default culture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With