Case-insensitive List<string>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Rae

    Case-insensitive List<string>

    Hi,

    Is it possible to create a case-insensitive List<stringcoll ection?

    E.g.

    List<stringMyLi st = new List<string>;
    MyList.Add("MyS tring");

    So that:

    MyList.Contains ("mystring")
    MyList.Contains ("MyString")
    MyList.Contains ("MYSTRING")

    all return true.

    Google shows plenty of examples of how to do this with Dictionary<stri ng,
    stringcollectio ns e.g.

    Dictionary<stri ng, stringMyDiction ary = new Dictionary<stri ng,
    string>(StringC omparer.Current CultureIgnoreCa se);

    but I can't find an equivalent for List<string>...

    Any assistance gratefully received.

    Mark


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Case-insensitive List&lt;string& gt;

    Mark,

    No, the List does not support any other comparison for Contains. You
    will have to use the dictionary method to determine if you have a
    case-insensitive string in your set.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Mark Rae" <mark@markNOSPA Mrae.comwrote in message
    news:OgdSVL%233 GHA.1068@TK2MSF TNGP05.phx.gbl. ..
    Hi,
    >
    Is it possible to create a case-insensitive List<stringcoll ection?
    >
    E.g.
    >
    List<stringMyLi st = new List<string>;
    MyList.Add("MyS tring");
    >
    So that:
    >
    MyList.Contains ("mystring")
    MyList.Contains ("MyString")
    MyList.Contains ("MYSTRING")
    >
    all return true.
    >
    Google shows plenty of examples of how to do this with Dictionary<stri ng,
    stringcollectio ns e.g.
    >
    Dictionary<stri ng, stringMyDiction ary = new Dictionary<stri ng,
    string>(StringC omparer.Current CultureIgnoreCa se);
    >
    but I can't find an equivalent for List<string>...
    >
    Any assistance gratefully received.
    >
    Mark
    >

    Comment

    • Andreas Mueller

      #3
      Re: Case-insensitive List&lt;string& gt;

      Mark Rae wrote:
      Hi,
      >
      Is it possible to create a case-insensitive List<stringcoll ection?
      >
      E.g.
      >
      List<stringMyLi st = new List<string>;
      MyList.Add("MyS tring");
      >
      So that:
      >
      MyList.Contains ("mystring")
      MyList.Contains ("MyString")
      MyList.Contains ("MYSTRING")
      >
      all return true.
      >
      Google shows plenty of examples of how to do this with Dictionary<stri ng,
      stringcollectio ns e.g.
      >
      Dictionary<stri ng, stringMyDiction ary = new Dictionary<stri ng,
      string>(StringC omparer.Current CultureIgnoreCa se);
      >
      but I can't find an equivalent for List<string>...
      >
      Any assistance gratefully received.
      >
      Mark
      >
      >
      No, but you can get the information you need from a regular List<string>:

      List<strings = new List<string>();
      bool contains = null != s.Find(delegate (string str)
      {
      return str.ToLower().E quals("MyString ".ToLower() );
      });


      HTH,
      Andy
      --
      You can email me directly by removing the NOSPAm below
      xmenNOSPAm40@gm xNOSPAm.netNOSPAm

      Comment

      • Chris Taylor

        #4
        Re: Case-insensitive List&lt;string& gt;

        Hi,

        Performing case-insensitive comparisons using ToLower/ToUpper are very
        expensive, a much more efficient means is to use String.Compare. For some
        non-English languages the ToLower solution is also not accurate/correct. See
        my post on case-insensitive string comparisons:


        --
        Chris Taylor

        "Andreas Mueller" <me@privacy.net wrote in message
        news:4nnplrFb5b 4aU1@individual .net...
        Mark Rae wrote:
        >
        >Hi,
        >>
        >Is it possible to create a case-insensitive List<stringcoll ection?
        >>
        >E.g.
        >>
        >List<stringMyL ist = new List<string>;
        >MyList.Add("My String");
        >>
        >So that:
        >>
        >MyList.Contain s("mystring")
        >MyList.Contain s("MyString")
        >MyList.Contain s("MYSTRING")
        >>
        >all return true.
        >>
        >Google shows plenty of examples of how to do this with Dictionary<stri ng,
        >stringcollecti ons e.g.
        >>
        >Dictionary<str ing, stringMyDiction ary = new Dictionary<stri ng,
        >string>(String Comparer.Curren tCultureIgnoreC ase);
        >>
        >but I can't find an equivalent for List<string>...
        >>
        >Any assistance gratefully received.
        >>
        >Mark
        No, but you can get the information you need from a regular List<string>:
        >
        List<strings = new List<string>();
        bool contains = null != s.Find(delegate (string str)
        {
        return str.ToLower().E quals("MyString ".ToLower() );
        });
        >
        >
        HTH,
        Andy
        --
        You can email me directly by removing the NOSPAm below
        xmenNOSPAm40@gm xNOSPAm.netNOSPAm

        Comment

        • Mark Rae

          #5
          Re: Case-insensitive List&lt;string& gt;

          "Chris Taylor" <chris_taylor_z a@hotmail.comwr ote in message
          news:%238McOh$3 GHA.5024@TK2MSF TNGP02.phx.gbl. ..
          Performing case-insensitive comparisons using ToLower/ToUpper are very
          expensive, a much more efficient means is to use String.Compare. For some
          non-English languages the ToLower solution is also not accurate/correct.
          See my post on case-insensitive string comparisons:
          http://dotnetjunkies.com/WebLog/chri.../07/21393.aspx
          Thanks everyone.


          Comment

          Working...