Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between InvariantCulture and Ordinal string comparison

When comparing two strings in c# for equality, what is the difference between InvariantCulture and Ordinal comparison?

like image 785
Kapil Avatar asked Jan 29 '09 18:01

Kapil


People also ask

What is an ordinal string comparison?

Ordinal comparisons are string comparisons in which each byte of each string is compared without linguistic interpretation; for example, "windows" does not match "Windows".

What is difference between InvariantCultureIgnoreCase and OrdinalIgnoreCase?

InvariantCultureIgnoreCase uses comparison rules based on english, but without any regional variations. This is good for a neutral comparison that still takes into account some linguistic aspects. OrdinalIgnoreCase compares the character codes without cultural aspects.

What is the best way to compare strings in C#?

Use the Equals() method to compare strings case-insensitive using StringComparison parameter. Always make sure that string is not null using null-conditional operator ? before calling Equals() method, as shown below.

What is StringComparison ordinal C#?

The StringComparison enumeration is used to specify whether a string comparison should use the current culture or the invariant culture, word or ordinal sort rules, and be case-sensitive or case-insensitive. Important. When you call a string comparison method such as String. Compare, String.


2 Answers

InvariantCulture

Uses a "standard" set of character orderings (a,b,c, ... etc.). This is in contrast to some specific locales, which may sort characters in different orders ('a-with-acute' may be before or after 'a', depending on the locale, and so on).

Ordinal

On the other hand, looks purely at the values of the raw byte(s) that represent the character.


There's a great sample at http://msdn.microsoft.com/en-us/library/e6883c06.aspx that shows the results of the various StringComparison values. All the way at the end, it shows (excerpted):

StringComparison.InvariantCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)  StringComparison.Ordinal: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) 

You can see that where InvariantCulture yields (U+0069, U+0049, U+00131), Ordinal yields (U+0049, U+0069, U+00131).

like image 68
JaredReisinger Avatar answered Oct 03 '22 20:10

JaredReisinger


It does matter, for example - there is a thing called character expansion

var s1 = "Strasse"; var s2 = "Straße";  s1.Equals(s2, StringComparison.Ordinal);           //false s1.Equals(s2, StringComparison.InvariantCulture);  //true 

With InvariantCulture the ß character gets expanded to ss.

like image 26
Ventsyslav Raikov Avatar answered Oct 03 '22 22:10

Ventsyslav Raikov