TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ToLower Method Examples, Performance

These C# examples use the ToLower method. They convert uppercase characters in strings to lowercase ones.

ToLower changes strings to be all lowercase.

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.

ToUpper

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.

Threads

Tip: You can find more information about using globalizations when lowercasing strings by reading about the TextInfo class.

TextInfo

Tip 2: If a string is already lowercase, we can simply do nothing. We must first scan for validity.

IsUpper, IsLower

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf