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

后端 未结 5 913
孤独总比滥情好
孤独总比滥情好 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:12

    First, you should decide whether you compare strings in culture dependent or independent way (e.g. in Russian Culture letters "E" and "Ё" often treats as being the same; Finnish tends to treat "V" and "W" as being the same etc.). Next you should choose whether use or not use case ("a" v. "A"). So there're 6 possible comparisons:

    Ordinal (culture independent) comparisons:

    // Ignore case comparison
    Boolean equals = String.Equals(x, y, StringComparison.OrdinalIgnoreCase);
    // Case comparison
    Boolean equals = String.Equals(x, y, StringComparison.Ordinal);
    

    Current culture comparisons:

    // Current culture, ignore case comparison
    Boolean equals = String.Equals(x, y, StringComparison.CurrentCulture);
    // Current culture, case comparison
    Boolean equals = String.Equals(x, y, StringComparison.CurrentCultureIgnoreCase);
    

    Explicit culture comparisons:

    CultureInfo culture = new CultureInfo("Ru-ru"); // <- Or whatever you want
    
    // Explicit culture, ignore case comparison
    Boolean equals = culture.CompareInfo.Compare(x, y, CompareOptions.IgnoreCase);
    // Explicit culture, case comparison
    Boolean equals = culture.CompareInfo.Compare(x, y, CompareOptions.None);
    
    0 讨论(0)
  • 2021-02-19 00:14

    The return value is not true but false since .NET is case sensitive by default.

    From String.Equals:

    This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

    For == the same is true since String.Equality operator calls Equals:

    This operator is implemented using the Equals method, which means the comparands are tested for a combination of reference and value equality. This operator performs an ordinal comparison.

    This will compare case insensitively:

    bool equals = x.Equals(y , StringComparison.OrdinalIgnoreCase);
    

    If you just want to know if a character is upper or lower case you can use these methods:

    bool isUpperChar = Char.IsUpper("ABC"[0]); // yes
    bool isLowerChar = Char.IsLower("ABC"[0]); // no
    
    0 讨论(0)
  • 2021-02-19 00:15

    This is another option you can try.

    if(string.Compare("a", "A", true) == 0)
    
    0 讨论(0)
  • 2021-02-19 00:16

    Try:

    Case sensitive:

    String.Equals (a,b)
    

    Case Insensitive

    string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
    
    0 讨论(0)
  • 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/

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