How to add a case-insensitive option to Array.IndexOf

后端 未结 7 1941
北海茫月
北海茫月 2020-12-10 01:08

I have a string

string str=\"hello\";

This is my array

string[] myarr = new string[] {\"good\",\"Hello\", \"this\" , \"new\         


        
相关标签:
7条回答
  • 2020-12-10 01:21
    var result = myarr.FindIndex(s => s.Equals(str, StringComparison.OrdinalIgnoreCase));
    
    0 讨论(0)
  • 2020-12-10 01:23

    Since Array.IndexOf is generic, it makes sense to make a generic extension function:

    public static int IndexOf<T>(this T[] source, T value)
    {
      return IndexOf<T>(source, value, StringComparison.InvariantCultureIgnoreCase);
    }
    
    public static int IndexOf<T>(this T[] source, T value, StringComparison stringComparison)
    {
      if (typeof(T) == typeof(string))
        return Array.FindIndex(source, m => m.ToString().Equals(value.ToString(), stringComparison));
      else
        return Array.IndexOf(source, value);
    }
    
    0 讨论(0)
  • 2020-12-10 01:24

    1) if you want to search only once and want to keep source array, you can use this:

            public static void example1()
            {
                string[] myarr = { "good", "Hello", "this", "new" };
                var str = "new";
                var res= Array.FindIndex(myarr, x=>string.Equals(x, str, StringComparison.InvariantCultureIgnoreCase));
            }
    

    2) if you will search many times it will be better to use this:

    public static void example1()
        {
            string[] myarr = {"good", "Hello", "this", "new"};
            var str = "new";
            var res = Array.IndexOf(Array.ConvertAll(myarr, ToStringlowerCase), str.ToLowerInvariant());
        }
    

    3) the answer above is incorrect :

    string array[] = {"hello", "hi", "bye" , "welcome" , "hell"}
    Int Indexofary = Array.FindIndex(array, t => t.IndexOf("hell", StringComparison.InvariantCultureIgnoreCase) >=0);
    

    will not work at all, because it searches not the string, but substring.

    Algorithm iterates words in array, when 1st word "hello" taken, algorithm tries to find index of 'hell' and this index is 1. 1 is > then 0 and algorithm will be finished without going to other words.

    If you don't want to search substrings but want to search strings, algorythm should be fixed. This algorithm can be fixed by adding check that substring starts from 1st char t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) == 0 and the length of the words equals str.Length == t.Length. Fixed:

            public static int example3()
        {
            string[] myarr = { "hHello", "hi", "bye", "welcome", "hell" };
            var str = "hell";
            return Array.FindIndex(myarr, t => t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) == 0 && str.Length == t.Length);
        }
    
    0 讨论(0)
  • 2020-12-10 01:31

    Since you are looking for index. Try this way.

    Array.FindIndex(myarr, t => t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) >=0);
    
    0 讨论(0)
  • 2020-12-10 01:34

    Array.IndexOf calls the default "Equals" method which is case-sensitive. Try this:

    Array.FindIndex(myarr, t => t.Equals(str, StringComparison.InvariantCultureIgnoreCase))
    
    0 讨论(0)
  • 2020-12-10 01:39

    I had a similar problem, I needed the index of the item but it had to be case insensitive, i looked around the web for a few minutes and found nothing, so I just wrote a small method to get it done, here is what I did:

    private static int getCaseInvariantIndex(List<string> ItemsList, string searchItem)
    {
        List<string> lowercaselist = new List<string>();
    
        foreach (string item in ItemsList)
        {
            lowercaselist.Add(item.ToLower());
        }
    
        return lowercaselist.IndexOf(searchItem.ToLower());
    }
    

    Add this code to the same file, and call it like this:

    int index = getCaseInvariantIndexFromList(ListOfItems, itemToFind);
    

    Hope this helps, good luck!

    0 讨论(0)
提交回复
热议问题