TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# char.ToLower and ToUpper

Call the char.ToLower method to make chars lowercase. Use ToUpper to transform to uppercase.
Char.ToLower. This 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.CharToLower
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.

Main: The 4 calls to char.ToLower show how this static method is used. The comments show the values returned by char.ToLower.

Next: The 4 calls to char.ToUpper show how this method is used. The result from each call is shown.

Static

Finally: The last statement in the code example is the most complex and it is a call to WriteLine with a format string.

Consolestring.Format
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
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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