String comparison: InvariantCultureIgnoreCase vs OrdinalIgnoreCase?

C#String

C# Problem Overview


Which would be better code:

int index = fileName.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase);

or

int index = fileName.LastIndexOf(".", StringComparison.OrdinalIgnoreCase);

C# Solutions


Solution 1 - C#

Neither code is always better. They do different things, so they are good at different things.

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. This is good for exact comparisons, like login names, but not for sorting strings with unusual characters like é or ö. This is also faster because there are no extra rules to apply before comparing.

Solution 2 - C#

FXCop typically prefers OrdinalIgnoreCase. But your requirements may vary.

For English there is very little difference. It is when you wander into languages that have different written language constructs that this becomes an issue. I am not experienced enough to give you more than that.

OrdinalIgnoreCase

> The StringComparer returned by the > OrdinalIgnoreCase property treats > the characters in the strings to > compare as if they were converted > to uppercase using the conventions > of the invariant culture, and then > performs a simple byte comparison > that is independent of language. > This is most appropriate when > comparing strings that are generated > programmatically or when comparing > case-insensitive resources such as > paths and filenames. http://msdn.microsoft.com/en-us/library/system.stringcomparer.ordinalignorecase.aspx

InvariantCultureIgnoreCase

> The StringComparer returned by the > InvariantCultureIgnoreCase property > compares strings in a linguistically > relevant manner that ignores case, but > it is not suitable for display in any > particular culture. Its major > application is to order strings in a > way that will be identical across > cultures. http://msdn.microsoft.com/en-us/library/system.stringcomparer.invariantcultureignorecase.aspx > > The invariant culture is the > CultureInfo object returned by the > InvariantCulture property. > > The InvariantCultureIgnoreCase > property actually returns an instance > of an anonymous class derived from the > StringComparer class.

Solution 3 - C#

If you really want to match only the dot, then StringComparison.Ordinal would be fastest, as there is no case-difference.

"Ordinal" doesn't use culture and/or casing rules that are not applicable anyway on a symbol like a ..

Solution 4 - C#

You seem to be doing file name comparisons, so I would just add that OrdinalIgnoreCase is closest to what NTFS does (it's not exactly the same, but it's closer than InvariantCultureIgnoreCase)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJL.View Question on Stackoverflow
Solution 1 - C#GuffaView Answer on Stackoverflow
Solution 2 - C#Sky SandersView Answer on Stackoverflow
Solution 3 - C#Hans KestingView Answer on Stackoverflow
Solution 4 - C#Dean HardingView Answer on Stackoverflow