3

I constantly read about it being a good practise to convert a string to upper case (I think Hanselman mentioned this on his blog a long time ago), when that string is to be compared against another (which should also be converted to upper case).

What is the benefit of this? Why should I do this (or are there any cases when I shouldn't)?

Thanks

gss3
  • 33
  • 1
  • 3
  • Possible duplicate of [C#: Confusion about ToUpper() and ToLower()](https://stackoverflow.com/questions/3694863/c-confusion-about-toupper-and-tolower) – Michael Freidgeim Sep 22 '17 at 23:26

7 Answers7

7

no, you should be using the enum option that allows for case insenstive comparisson (string comparison).

Make sure to use that overload of the comparison method you are using i.e. String.Compare, String.Equals

eglasius
  • 35,831
  • 5
  • 65
  • 110
6

The reason that you should convert to upper case rather than lower case when doing a comparison (and it's not practically possible to do a case insensetive comparison), is that some (not so commonly used) characters does not convert to lower case without losing information.

Some upper case characters doesn't have an equivalent lower case character, so making them lower case would convert them into a different lower case character. That could cause a false positive in the comparison.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

A better way to do case-insensitive string comparison is:

bool ignoreCase = true;
bool stringsAreSame = (string.Compare(str1, str2, ignoreCase) == 0) 

Also, see here: Upper vs Lower Case

Community
  • 1
  • 1
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
1

Strings should be normalized to uppercase. A small group of characters, when they are converted to lowercase, cannot make a round trip. To make a round trip means to convert the characters from one locale to another locale that represents character data differently, and then to accurately retrieve the original characters from the converted characters.

Reference:

https://learn.microsoft.com/en-us/visualstudio/code-quality/ca1308-normalize-strings-to-uppercase?view=vs-2015][1]

Zoren Konte
  • 306
  • 4
  • 12
0

This sounds like a cheap way to do case-insensitive comparisons. I would wonder if there isn't a function that would do that for you, without you having to explicitly telling it to go uppercase.

Thanatos
  • 42,585
  • 14
  • 91
  • 146
0

The .Net framework is slightly faster at doing string comparisons between uppercase letters than string comparisons between lowercase letters.

As others have mentioned, some information might be lost when converting from uppercase to lowercase.

You may want to try using a StringComparer object to do case insensitive comparisons.

StringComparer comparer = StringComparer.OrdinalIgnoreCase;
bool isEqualV1 = comparer.Equals("stringA", "stringB");
bool isEqualV2 = (comparer.Compare("stringA", "stringB") == 0);

The .Net Framework as of of 4.7 has a Span type that should assist in speeding up string comparisons in certain circumstances.

Depending on your use case, you may want to make use of the constructors for HashSet and Dictionary types which can take a StringComparer as an input parameter for the constructor.

I typically use a StringComparer as an input parameter to a method with a default of StringComparer.OrdingalIgnoreCase and I try to make use of other techniques (use of HashSets, Dictionaries or Spans) if speed is important.

Gaspare Bonventre
  • 1,134
  • 11
  • 19
-3

You do not have to convert a string to Upper case. Convert it to lower case 8-)

Andrei Drynov
  • 8,362
  • 6
  • 39
  • 39