What is the correct way to compare char ignoring case?

前端 未结 9 2475
不知归路
不知归路 2020-12-01 20:31

I\'m wondering what the correct way to compare two characters ignoring case that will work for all cultures. Also, is Comparer.Default the best way

9条回答
  •  执笔经年
    2020-12-01 20:58

    Using the default (that is not the invariant) culture:

    if (char.ToLower(ch1) == char.ToLower(ch2))
    {  ....  }
    

    Or specify a culture:

    CultureInfo myCulture = ...;
    if (char.ToLower(ch1, myCulture) == char.ToLower(ch2, myCulture))
    {  ....  }
    

提交回复
热议问题