TheDeveloperBlog.com

Home | Contact Us

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

C# char.ToLower Methods

This C# example program uses char.ToLower to make characters lowercase.

Char.ToLower. The char.ToLower method converts only uppercase letters.

It changes uppercase letters to lowercase letters. It leaves all other characters unchanged. It is ideal for character conversions—it is included in the .NET Framework.

ToLower

Example. Here you see that char.ToLower converts uppercase characters to lowercase characters, while not changing characters that are already lowercase or digits. In the same example, you see char.ToUpper, which is the opposite.

C# program that uses char.ToLower

using System;

class Program
{
    static void Main()
    {
	char c1 = 'a'; // Lowercase a
	char c2 = 'b'; // Lowercase b
	char c3 = 'C'; // Uppercase C
	char c4 = '3'; // Digit 3

	char lower1 = char.ToLower(c1); // a
	char lower2 = char.ToLower(c2); // b
	char lower3 = char.ToLower(c3); // c [changed]
	char lower4 = char.ToLower(c4); // 3

	char upper1 = char.ToUpper(c1); // A [changed]
	char upper2 = char.ToUpper(c2); // B [changed]
	char upper3 = char.ToUpper(c3); // C
	char upper4 = char.ToUpper(c4); // 3

	Console.WriteLine("{0},{1},{2},{3}\n" +
	    "{4},{5},{6},{7}\n" +
	    "{8},{9},{10},{11}",
	    c1, c2, c3, c4,
	    lower1, lower2, lower3, lower4,
	    upper1, upper2, upper3, upper4);
    }
}

Output

a,b,C,3
a,b,c,3
A,B,C,3

In this example, the four calls to char.ToLower show how this static method is used. The comments show the values returned by char.ToLower. Next, the four calls to char.ToUpper show how this static method is used.

Static Method

Finally, the last statement in the code example is the most complex and it is a call to WriteLine with a format string. The 12 variables are substituted into the string, which is then displayed.

Console.WriteLinestring.Format

Discussion. Sometimes it is better to first test if a character is lowercase or uppercase. Fortunately, the .NET Framework provides the char.IsLower and char.IsUpper methods, which also alias the System.Char type and are static methods.

char.IsLower

Tip: These four static methods use value semantics, which means the parameter is copied to the method as a value.

Therefore: The ToLower and ToUpper methods do not modify the variable you use—they return a new value.

Also, these two methods use the CultureInfo.InvariantCulture parameter internally. This is not usually a big concern. It means that the letters are lowercased and uppercased the same in all globalization settings.

So: If you deploy your app to a customer in Turkey, all the letters will be lowercase the same as in India.

Finally, the char.ToLower and char.ToUpper methods have worse performance than a custom method that tests ASCII values. If the primary purpose of your code is to convert character cases, then it is worthwhile to use a custom method.

Char Lowercase Optimization

Summary. We saw examples of using char.ToLower and char.ToUpper. These are simple but useful methods for quickly testing characters. We saw how to test the results and write them to the Console.


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