Comparing two strings, ignoring case in C#

C#StringComparisonEquals

C# Problem Overview


Which of the following two is more efficient? (Or maybe is there a third option that's better still?)

string val = "AStringValue";

if (val.Equals("astringvalue", StringComparison.InvariantCultureIgnoreCase))

OR

if (val.ToLowerCase() == "astringvalue")

?

C# Solutions


Solution 1 - C#

If you're looking for efficiency, use this:

string.Equals(val, "astringvalue", StringComparison.OrdinalIgnoreCase)

Ordinal comparisons can be significantly faster than culture-aware comparisons.

ToLowerCase can be the better option if you're doing a lot of comparisons against the same string, however.

As with any performance optimization: measure it, then decide!

Solution 2 - C#

The first one is the correct one, and IMHO the more efficient one, since the second 'solution' instantiates a new string instance.

Solution 3 - C#

The .ToLowerCase version is not going to be faster - it involves an extra string allocation (which must later be collected), etc.

Personally, I'd use

string.Equals(val, "astringvalue",  StringComparison.OrdinalIgnoreCase)

this avoids all the issues of culture-sensitive strings, but as a consequence it avoids all the issues of culture-sensitive strings. Only you know whether that is OK in your context.

Using the string.Equals static method avoids any issues with val being null.

Solution 4 - C#

My general answer to this kind of question on "efficiency" is almost always, which ever version of the code is most readable, is the most efficient.

That being said, I think (val.ToLowerCase() == "astringvalue") is pretty understandable at a glance by most people.

The efficience I refer to is not necesseraly in the execution of the code but rather in the maintanance and generally readability of the code in question.

Solution 5 - C#

I'd venture that the safest is to use String.Equals to mitigate against the possibility that val is null.

Solution 6 - C#

The former is fastest. Turns out that val is immutable, and so a new string object is created with String.ToLowerCase(), rather than just direct comparison with the string comparer. Creating a new string object can be costly if you're doing this many times a second.

Solution 7 - C#

Solution 8 - C#

1st is more efficient (and the best possible option) because val.ToLowerCase() creates a new object since Strings are immutable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpwmusicView Question on Stackoverflow
Solution 1 - C#SvenView Answer on Stackoverflow
Solution 2 - C#Frederik GheyselsView Answer on Stackoverflow
Solution 3 - C#Marc GravellView Answer on Stackoverflow
Solution 4 - C#Jamie DixonView Answer on Stackoverflow
Solution 5 - C#spenderView Answer on Stackoverflow
Solution 6 - C#foxyView Answer on Stackoverflow
Solution 7 - C#Tomasz JaskuλaView Answer on Stackoverflow
Solution 8 - C#RuslanView Answer on Stackoverflow