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

后端 未结 7 1942
北海茫月
北海茫月 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:40

    Beaware !! The Answer marked might have some problem , like

    string array[] = {"hello", "hi", "bye" , "welcome" , "hell"}
    

    if you use the same method as described in the answer to find the index of word "hell"

    Int Indexofary = Array.FindIndex(array, t => t.IndexOf("hell", StringComparison.InvariantCultureIgnoreCase) >=0);
    

    you will get result Indexofary = 0 instead of 4.

    Instead of that use

    Array.FindIndex(array, t => t.Equals("hell", StringComparison.InvariantCultureIgnoreCase));
    

    to get proper result .

    Rrgards Bits

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