How to compare two strings and their upper and lower case signs

后端 未结 5 915
孤独总比滥情好
孤独总比滥情好 2021-02-18 23:47

Let\'s say I have 2 strings.First string is x = \"abc\" and the second one is y = \"ABC\".When I write in c# following code:

if(x == y)

or

5条回答
  •  后悔当初
    2021-02-19 00:20

    As Pleun wrote, or you can

    StringComparer.CurrentCultureIgnoreCase.Equals(a, b)
    

    Note that we are using the CurrentCulture ordering method. Sometimes you'll have to use different ordering methods (every language orders the letters in a different way)

    If you are sure that you are only ordering ASCII characters then

    StringComparer.OrdinalIgnoreCase.Equals(a, b)
    

    is a little faster (or in general methods where you can select the OrdinalIgnoreCase)

    In general converting ToUpper() or ToLower() two strings to compare them is wrong (and slow, because you have to convert them fully before comparing them, while perhaps they are different in the first character)... Wrong because in Turkish there are four i

    http://codeblog.jonskeet.uk/2009/11/02/omg-ponies-aka-humanity-epic-fail/

提交回复
热议问题