Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 682 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String comparison: InvariantCultureIgnoreCase vs OrdinalIgnoreCase?

#1
Which would be better code:

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

or

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



Reply

#2
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`)
Reply

#3
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.

[To see links please register here]


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.

[To see links please register here]

>
> 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.

Reply

#4
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.
Reply

#5
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 `.`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through