C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It converts an entire string—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.
Example. First here we call ToLower on a mixed-case or uppercase string and it will return a copied version that is all lowercase. Characters such as digits will not be modified. Only uppercase letters are changed.
C# program that uses ToLower 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.
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. The Console.WriteLine method is called with a format string.
Console.WriteLinestring.Format
Example 2. Next, we 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.
C# program that uses System.Globalization 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
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: The program calls the ToLower instance method overload with one parameter.
Note: Internally, this method doesn't need to fetch the CurrentCulture because it already has it.
Performance. 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]
When we get the CultureInfo of the CurrentCulture, we access a property that internally gets Thread.CurrentThread.CurrentCulture. We access per-thread data. This explains the speedup when declaring CultureInfo outside of the loop.
Tip: You can find more information about using globalizations when lowercasing strings by reading about the TextInfo class.
Tip 2: If a string is already lowercase, we can simply do nothing. We must first scan for validity.
Summary. We used the ToLower method in the C# language targeting the .NET Framework, including both overloads. We also used ToLowerInvariant—this was much slower than the ToLower method. ToLower with no parameter was not the fastest.