20

Let's say I have 2 strings. First string is x = "abc" and the second one is y = "ABC". In C# when I write the following code:

if (x == y)

or

if (x.Equals(y))

the return value is true. How can I check their upper and lower case?

Andreas
  • 5,393
  • 9
  • 44
  • 53
daidai
  • 531
  • 5
  • 10
  • 22

5 Answers5

32

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
8

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);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • compare method return c like int. not bool an if statement should look like below: if (culture.CompareInfo.Compare(PaymentReq.IslemTipi, "ödeme", CompareOptions.IgnoreCase) == 0) – cahit beyaz Aug 16 '17 at 06:07
2

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/

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

Try:

Case sensitive:

String.Equals (a,b)

Case Insensitive

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
Pleun
  • 8,856
  • 2
  • 30
  • 50
1

This is another option you can try.

if(string.Compare("a", "A", true) == 0)
Krishna Sarma
  • 1,852
  • 2
  • 29
  • 52