Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.ToLower() and string.ToLowerInvariant()

Tags:

string

c#

.net

People also ask

What does string ToLower do?

Returns. A string in lowercase.

What is ToLower () in C#?

In C#, ToLower() is a string method. It converts every character to lowercase (if there is a lowercase character). If a character does not have a lowercase equivalent, it remains unchanged.

Is ToUpper faster than ToLower?

The other three are mostly the same. But in general, ToLowerInvariant is fastest, then ToUpper and then ToUpperInvariant . Surprisingly, the . NET Core is roughly 2,6× faster across the results.

What is ToUpperInvariant C#?

ToUpperInvariant() method in C# is used to return a copy of this String object converted to uppercase using the casing rules of the invariant culture.


Depending on the current culture, ToLower might produce a culture specific lowercase letter, that you aren't expecting. Such as producing ınfo without the dot on the i instead of info and thus mucking up string comparisons. For that reason, ToLowerInvariant should be used on any non-language-specific data. When you might have user input that might be in their native language/character-set, would generally be the only time you use ToLower.

See this question for an example of this issue: C#- ToLower() is sometimes removing dot from the letter "I"


TL;DR:

When working with "content" (e.g. articles, posts, comments, names, places, etc.) use ToLower(). When working with "literals" (e.g. command line arguments, custom grammars, strings that should be enums, etc.) use ToLowerInvariant().

Examples:

=Using ToLowerInvariant incorrectly=

In Turkish, DIŞ means "outside" and diş means "tooth". The proper lower casing of DIŞ is dış. So, if you use ToLowerInvariant incorrectly you may have typos in Turkey.

=Using ToLower incorrectly=

Now pretend you are writing an SQL parser. Somewhere you will have code that looks like:

if(operator.ToLower() == "like")
{
  // Handle an SQL LIKE operator
}

The SQL grammar does not change when you change cultures. A Frenchman does not write SÉLECTIONNEZ x DE books instead of SELECT X FROM books. However, in order for the above code to work, a Turkish person would need to write SELECT x FROM books WHERE Author LİKE '%Adams%' (note the dot above the capital i, almost impossible to see). This would be quite frustrating for your Turkish user.


I think this can be useful:

http://msdn.microsoft.com/en-us/library/system.string.tolowerinvariant.aspx

update

If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToLowerInvariant method. The ToLowerInvariant method is equivalent to ToLower(CultureInfo.InvariantCulture). The method is recommended when a collection of strings must appear in a predictable order in a user interface control.

also

...ToLower is very similar in most places to ToLowerInvariant. The documents indicate that these methods will only change behavior with Turkish cultures. Also, on Windows systems, the file system is case-insensitive, which further limits its use...

http://www.dotnetperls.com/tolowerinvariant-toupperinvariant

hth


String.ToLower() uses the default culture while String.ToLowerInvariant() uses the invariant culture. So you are essentially asking the differences between invariant culture and ordinal string comparision.