4

Doing plenty of case-insensitive string comparisons, I end of with lots of lengthy statements like:

myList.FirstOrDefault(
    c => string.Equals(
             c.InternalName, 
             internalName, 
             StringComparison.InvariantCultureIgnoreCase));

What bothers me is the long name of StringComparison.InvariantCultureIgnoreCase.

I could think of having extension methods and the like to shorten the code I have to write but on the other hand I fear of "obfuscating" my code and thus making it harder to understand.

So my question is:

Is there a "best practice" of having to write less text and still do InvariantCultureIgnoreCase-style string comparisons?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Side point, are you aware of the difference between Ordinal and Invariant comparisons: http://stackoverflow.com/questions/2256453/using-invariantcultureignorecase-instead-of-toupper-for-case-insensitive-string/2256491#2256491 and Microsoft's recommendations: http://msdn.microsoft.com/en-us/library/ms973919.aspx – weston Jun 06 '14 at 08:19
  • @weston Thanks. Seems I should use `OrdinalIgnoreCase` instead (which is somewhat shorter, too!). – Uwe Keim Jun 06 '14 at 08:20
  • 1
    I'm using an extension method, which works pretty well and I don't think it obfuscates my code. My team mates also comfortable using it. Namely `EqualsIgnoreCase` – Sriram Sakthivel Jun 06 '14 at 08:24

4 Answers4

4

You could wrap it around in an Extension Method:

public static class StringExtensions
{
    public static bool EqualsCaseInsensitive(this string str, string other)
    {
        return string.Equals(str, other, StringComparison.InvariantCultureIgnoreCase);
    }
}

and then do:

myList.FirstOrDefault(
    c => c.InternalName.EqualsCaseInsensitive(internalName))
facundofarias
  • 2,973
  • 28
  • 27
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2

You could do:

myList.FirstOrDefault(
    c => string.Equals(
             c.InternalName, 
             internalName, 
             (StringComparison)3);

Anyway... I think you should not worry about this kind of things.

EDIT:

If you want to make it really short, you can do something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using s = System.String;
using sc = System.StringComparison;
using l = System.Collections.Generic.List<Test.Test.c>;

namespace Test
{
    class Test
    {
        private sc cic = sc.InvariantCultureIgnoreCase;

        public class c
        {
            public s i;
        }

        private void Test(s i)
        {
            l ml = new l();

            ml.FirstOrDefault(
                c => s.Equals(
                         c.i, 
                         i,
                         cic));
        }
    }
}

:-)

HABJAN
  • 9,212
  • 3
  • 35
  • 59
2

You can always assign it to a constant :

public const StringComparison IGNORE_CASE= StringComparison.InvariantCultureIgnoreCase; //use whatever name you like

And place it somewhere where it can be referenced from any place you need it...(like in an "utility" dll if you need to use it in several projects)

Now you can use it instead of the big enum

myList.FirstOrDefault(
    c => string.Equals(
         c.InternalName, 
         internalName, 
         IGNORE_CASE));
Renincuente
  • 150
  • 9
1

Not aware of any short cut in the framework. If you find yourself repeating any code you should make a function. If that is a static function is useful across many places, then move it to a util class:

myList.FirstOrDefault(
    c => Are.SameIgnoringCase(
             c.InternalName, 
             internalName));

public static class Are {
  public boolean SameIgnoringCase(string a, string b)
  {
    return string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
  }
}

I have given the class a rather cryptic name, but it leads to nice readable code.

I have included a change which follows Microsoft recommendations as you probably don't require invariant comparison. See also this: Using InvariantCultureIgnoreCase instead of ToUpper for case-insensitive string comparisons

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203