3

I want to compare two stings case insensitive but I'm not sure about the best way to do it. The strings have a average length of 20 and this question is more about state of the art and not about best performance

Most of my code uses

bool output = "foo".ToLower() == "FOO".ToLower();

which seems a bit old fashioned to me. Another way I've seen many times is

bool output = Regex.IsMatch("foo", "FOO", RegexOptions.IgnoreCase);

I guess that's possible but RegEx is not made for such simple things.

After that 3 "good" way are left:

bool output = string.Compare("foo", "FOO", StringComparison.CurrentCultureIgnoreCase) == 0;
bool output = string.Compare("foo", "FOO", true) == 0;
bool output = "foo".Equals("FOO", StringComparison.CurrentCultureIgnoreCase);
Toshi
  • 2,532
  • 4
  • 17
  • 45
  • This is an opinion based question. But in my opinion: `string.Equals("x", "y", StringComparison.OrdinalIgnoreCase)` – Gilad Green Oct 06 '16 at 06:53
  • Source IndexOf not an option? – Prix Oct 06 '16 at 06:54
  • Also look [here](http://stackoverflow.com/questions/3121957/how-can-i-do-a-case-insensitive-string-comparison) and [here](http://stackoverflow.com/questions/6371150/comparing-two-strings-ignoring-case-in-c-sharp) – Gilad Green Oct 06 '16 at 06:59
  • what do you mean, "the best"? best performance-wise, shortest, most readable? – nicks Oct 06 '16 at 07:00
  • @NikaGamkrelidze it's all sayed in the 2nd sentence – Toshi Oct 06 '16 at 08:30

3 Answers3

4

If you have a look at the corresponding reference sources

https://referencesource.microsoft.com/#mscorlib/system/string.cs,bda3b2c94b5251ce

    public static int Compare(String strA, String strB, bool ignoreCase)
    {
        if (ignoreCase) {
            return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase);
        }
        return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None);
    }

https://referencesource.microsoft.com/#mscorlib/system/string.cs,0be9474bc8e160b6

    public static int Compare(String strA, String strB, StringComparison comparisonType) 
    {
    ... 
        // Agrument validation, reference equality, null test

        switch (comparisonType) {
        ...
           case StringComparison.CurrentCultureIgnoreCase:
                return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase);

https://referencesource.microsoft.com/#mscorlib/system/string.cs,d47c1f57ad1e1e6e

    public static bool Equals(String a, String b, StringComparison comparisonType) {
    ... 
       // Agrument validation, reference equality, null test

       switch (comparisonType) {
        ...
           case StringComparison.CurrentCultureIgnoreCase:
                return (CultureInfo.CurrentCulture.CompareInfo.Compare(a, b, CompareOptions.None) == 0);

you'll find these three methods being equal one another. As for other ways, Regex.IsMatch is definitely an overshoot (all you have to do is to compare strings); ToLower() can be tricky when dealing with culture specific letters, see

https://en.wikipedia.org/wiki/Dotted_and_dotless_I

that's why a better design is to declare your intends clearly (= I want to compare strings) then mask them (and let the system decieve you)

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

If you're checking for equality, use Equals. Using Compare is equaivalent to, say, using

if (collection.Count() == 0)

instead of

if (collection.Any())

in LINQ. Even if it the two may currently be implemented and perform similarly, you're masking your intent and you've got no guarantee of that being the case in future.

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • but `Compare` does also determine a sorting order (-1,0,1) and `Equals` is only about checking equality right? – Toshi Oct 06 '16 at 08:29
  • Exactly. If you are concerned about whether two items are equal, you should be calling the method that checks whether they are equal, not the one that checks whether one is bigger or smaller than the other. – Rawling Oct 06 '16 at 10:13
-2

i think it is good

bool output = Regex.IsMatch("foo", "FOO", RegexOptions.IgnoreCase);
Manish Singh
  • 934
  • 1
  • 12
  • 27
  • 6
    I think regex is the last thing you should ever consider, it's only good when all else fails. Which shows that this question is opinion-based and thus off-topic here – slawekwin Oct 06 '16 at 07:01