ToLower
, ToUpper
Every letter has an uppercase and lowercase form. With the C# ToLower
and ToUpper
methods on the string
type, we can convert cases.
ToLower
changes strings to be all lowercase. It converts an entire string
—without changing letters that are already lowercased or digits.
ToLower
exampleWe call ToLower
on a string
—it will return a copied version that is all lowercase. Characters such as digits will not be modified. Only uppercase letters are changed.
string
is declared and the instance method ToLower
is called on it. That method returns a new string
that is lowercase.string
is not modified. The Console.WriteLine
method is called with a format string
.ToLower
copies a string
and returns a reference to the new string
. The original string
is unchanged.using System; // Input string. string mixedCase = "CATx2"; // Call ToLower method. // ... This returns a new string copy. string lower = mixedCase.ToLower(); // Display results. Console.WriteLine("BEFORE: " + mixedCase); Console.WriteLine(" AFTER: " + lower);BEFORE: CATx2 AFTER: catx2
CultureInfo
Next we use CultureInfo
with ToLower
. This example has improved performance over the code in the first example. This is shown in the benchmark later.
string
is declared, we declare a new CultureInfo
, which we acquire from the CurrentCulture
property.ToLower
with one parameter.CurrentCulture
because it already has it.using System; using System.Globalization; class Program { static void Main() { // Input string. string upper = "UPPERCASE STRING"; // Get current culture. CultureInfo culture = CultureInfo.CurrentCulture; // Call ToLower instance method with globalization parameter. string lower = upper.ToLower(culture); // Display result. Console.WriteLine(lower); } }uppercase string
ToUpper
exampleToUpper
uppercases all letters in a string
. It is useful for processing text input or for when you need to check the string
against an already uppercase string
.
ToUpper
is an instance method on the string
type, which means you must have a string
variable instance to call it.ToUpper
works the same as ToLower
except it changes lowercase characters to uppercase characters.CultureInfo.InvariantCulture
class
specifies we want the string
to be uppercased the same way on all computers.using System; using System.Globalization; class Program { static void Main() { // // Uppercase this mixed case string. // string value1 = "Lowercase string."; string upper1 = value1.ToUpper(); Console.WriteLine(upper1); // // Uppercase this string. // string value2 = "ABC123"; string upper2 = value2.ToUpper(CultureInfo.InvariantCulture); Console.WriteLine(upper2); } }LOWERCASE STRING. ABC123
ToLowerInvariant
ToLowerInvariant
and ToUpperInvariant
affect strings differently than ToLower
and ToUpper
. The word "invariant" indicates the system's culture has no effect on the result.
using System; class Program { static void Main() { // This demonstrates the invariant methods. // ... They act in the expected way. string test1 = "Cat"; Console.WriteLine(test1.ToLowerInvariant()); Console.WriteLine(test1.ToUpperInvariant()); } }cat CAT
Here I tested the performance of ToLower
against the performance of ToLowerInvariant
. I found a difference between the 2 methods.
ToLower
with no arguments to lowercase the text string
.ToLowerInvariant
to lowercase the string
.ToLower
with a CultureInfo
argument (CurrentCulture
) to lowercase the string
data.ToLowerInvariant
method seems to perform the fastest.using System; using System.Diagnostics; using System.Globalization; string text = "This is an UPPER string."; CultureInfo c = CultureInfo.CurrentCulture; const int m = 1000000; // Version 1: ToLower with no arguments. Stopwatch s1 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { string text2 = text.ToLower(); if (text2 == null) { return; } } s1.Stop(); // Version 2: ToLowerInvariant. Stopwatch s2 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { string text2 = text.ToLowerInvariant(); if (text2 == null) { return; } } s2.Stop(); // Version 3: ToLower with CultureInfo argument. Stopwatch s3 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { string text2 = text.ToLower(c); if (text2 == null) { return; } } s3.Stop(); Console.WriteLine(s1.ElapsedMilliseconds); Console.WriteLine(s2.ElapsedMilliseconds); Console.WriteLine(s3.ElapsedMilliseconds);55 ms ToLower 35 ms ToLowerInvariant 34 ms ToLower(CultureInfo)
IsLower
, IsUpper
If a string
is already lowercase, we can simply do nothing. We must first scan for validity. This optimization can help if our data is usually already lowercase.
We used ToLower
and ToUpper
to transform the casing of the letters in C# strings. We also used and timed ToLowerInvariant
.