C# ToLower

Lowercase and uppercase words

ToLower changes strings to be all lowercase. It converts an entire string to lowercase—without changing letters that are already lowercased or digits. It copies a string and returns a reference to the new string. The original string is unchanged.

These C# examples show several examples and options for the ToLower method.

Example

Note

First here we see how you can call ToLower on your mixed-case or uppercase string and it will return a copied version that is all lowercase. Note that characters such as digits will not be modified.

Program that uses ToLower [C#]

using System;

class Program
{
    static void Main()
    {
	// Input string
	string mixedCase = "This is a MIXED case string.";

	// Call ToLower instance method, which returns a new copy.
	string lower = mixedCase.ToLower();

	// Display results
	Console.WriteLine("{0}, {1}",
	    mixedCase,
	    lower);
    }
}

Output

This is a MIXED case string., this is a mixed case string.

Description. The first string is declared and the instance method ToLower is called on it. That method returns a new string that is lowercase. The original string is not modified. Next, the Console.WriteLine method is called with a format string that displays the two strings separated by a comma to the console.

Globalization and CultureInfo

Here we see how you can use CultureInfo with ToLower. I don't show any real-world globalization code here. I include this example because it has improved performance over the code in the first example, as we see in the benchmark later.

Program that uses System.Globalization [C#]

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);
    }
}

Output

uppercase string

Description. You can see that after the first string is declared, we declare a new CultureInfo, which we acquire from the CurrentCulture property. Internally, this property fetches the current thread's globalization state.

Next steps. The program calls the ToLower instance method overload with one parameter. Internally, this method doesn't need to fetch the CurrentCulture because it already has it. Finally, the lowercased string is written to the console.

Benchmark

Performance optimization

Here I tested the performance of ToLower versus the performance of ToLowerInvariant. I was surprised to find a big difference between the two methods, with ToLower being faster. I found that using an explicit CultureInfo was fastest.

Input variables used in benchmark [C#]

string text = "This is an UPPER string.";
CultureInfo c = CultureInfo.CurrentCulture;

Statements tested in loops [C#]
    Each line was in a separate loop.
    10000000 iterations.

string text2 = text.ToLower();

string text2 = text.ToLowerInvariant();

string text2 = text.ToLower(c);

Results

ToLower():            1054 ms
ToLowerInvariant():   1724 ms
ToLower(CultureInfo):  884 ms [fastest]

Results. When you get the CultureInfo of the CurrentCulture, you are accessing a property that gets Thread.CurrentThread.CurrentCulture internally. So, you are actually accessing per-thread data. This accounts for the performance improvement when declaring CultureInfo outside of the loop. You can find more information about using globalizations when lowercasing strings by reading about the TextInfo class, which is described at this site.

TextInfo Method Tips

Advanced optimization

Programming tip

If you have an ASCII-only string, you can use a more optimized ToLower method that does not handle Unicode. This site has a complete benchmark on this.

ToLower Optimization

ToUpper

String type

The ToUpper method on the string type in the C# programming language is also useful, and is called in almost the exact same way. There is a separate article on ToUpper, which has more detailed discussion on the specific topic.

ToUpper Method

Summary

The C# programming language

We saw how to use the ToLower method in the C# language targeting the .NET Framework, including both overloads. Additionally, we used ToLowerInvariant. ToLowerInvariant was much slower that ToLower, while ToLower with no parameter was not the fastest.

String Type
.NET