gss3
gss3

Reputation: 41

Why should I convert a string to upper case when comparing?

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

Upvotes: 3

Views: 4759

Answers (7)

Zoren Konte
Zoren Konte

Reputation: 366

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]

Upvotes: 1

Gaspare Bonventre
Gaspare Bonventre

Reputation: 1154

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.

Upvotes: 0

Guffa
Guffa

Reputation: 700562

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.

Upvotes: 6

Andrei Drynov
Andrei Drynov

Reputation: 8592

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

Upvotes: -3

Chris Fulstow
Chris Fulstow

Reputation: 41902

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

Upvotes: 1

eglasius
eglasius

Reputation: 36035

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

Upvotes: 7

Thanatos
Thanatos

Reputation: 44334

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.

Upvotes: 0

Related Questions