Line sensitivity

Possible duplicate:
Is C # register invalid equal to operator?

string string1 = "aBc"

string string2 = "AbC"

how to check if string1 matches string2 and return true, regardless of case sensitivity.

+3
source share
5 answers

Two approaches:

You can .ToLower()either do string equality, or you can use this:

string.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase)

: downvoters, , (.. , ). , ( , , .NET Unicode), :

string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase)
+10

MSDN: " " :

  • : StringComparison.Ordinal OrdinalIgnoreCase .
  • DO: StringComparison.Ordinal OrdinalIgnoreCase .
  • DO: StringComparison.CurrentCulture .
  • DO: StringComparison.Ordinal StringComparison.OrdinalIgnoreCase, (, ).
  • DO: ToUpperInvariant, ToLowerInvariant.
  • : , .
  • : StringComparison.InvariantCulture; , - .

, . .

+3

You can also use string.Compare by adding a third parameter, which is ignoreCase:

if (string.Compare(string1, string2, true) == 0) 
{ 
   // string are equal
}

And you can also use the CompareInfo class:

if (CultureInfo.CurrentCulture.CompareInfo.Compare(string1, string2, 
    CompareOptions.IgnoreCase) == 0)
{
   // string are equal
}
+2
source
string.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase);

: D

+1
source
string.Equals("aBc", "AbC", StringComparison.CurrentCultureIgnoreCase) 
+1
source

Source: https://habr.com/ru/post/1717029/


All Articles