Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between the different overloads of String.Compare

Concretely what is the difference between

  • String.Compare(String, String, StringComparison) and
  • String.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.

like image 353
Guillaume Avatar asked May 18 '12 12:05

Guillaume


People also ask

What is the correct way to compare string variables?

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.

How do you compare string sizes?

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);

Do comparison operators work on strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.


1 Answers

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.

like image 57
Thinking Sites Avatar answered Nov 01 '22 13:11

Thinking Sites