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# ToLower and ToUpper: Uppercase and Lowercase Strings

Use the ToLower and ToUpper methods. Convert uppercase characters in strings to lowercase ones.
ToLower, ToUpper. A cat has 2 states: sleeping and awake. In a similar way we have uppercase and lowercase letters. With ToLower and ToUpper we can convert cases.StringsCastsUppercase First Letter
Method notes. ToLower changes strings to be all lowercase. It converts an entire string—without changing letters that are already lowercased or digits.
ToLower example. We call ToLower on a string. It will return a copied version that is all lowercase. Characters such as digits will not be modified. Only uppercase letters are changed.

Tip: The first string is declared and the instance method ToLower is called on it. That method returns a new string that is lowercase.

Important: The original string is not modified. The Console.WriteLine method is called with a format string.

Consolestring.Format

Copy: ToLower copies a string and returns a reference to the new string. The original string is unchanged.

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.
Example, CultureInfo. Next we use CultureInfo with ToLower. This example has improved performance over the code in the first example. This is shown in the benchmark later.

Tip: You can see that after the first string is declared, we declare a new CultureInfo, which we acquire from the CurrentCulture property.

Finally: Internally this property fetches the current thread's globalization state. The program calls ToLower with one parameter.

Info: Internally this method doesn't need to fetch the CurrentCulture because it already has it.

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
Benchmark. Here I tested the performance of ToLower against the performance of ToLowerInvariant. I found a difference between the 2 methods.

Version 1: This code uses ToLower with no arguments to lowercase the text string.

Version 2: This version of the code uses ToLowerInvariant to lowercase the string.

Version 3: Here we use ToLower with a CultureInfo argument (CurrentCulture) to lowercase the string data.

Result: Currently, in 2019, we find that the ToLowerInvariant method seems to perform the fastest.

C# program that benchmarks lowercase methods using System; using System.Diagnostics; using System.Globalization; class Program { static void Main() { string text = "This is an UPPER string."; CultureInfo c = CultureInfo.CurrentCulture; const int m = 1000000; // Version 1: ToLower with no arguments. Stopwatch s1 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { string text2 = text.ToLower(); if (text2 == null) { return; } } s1.Stop(); // Version 2: ToLowerInvariant. Stopwatch s2 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { string text2 = text.ToLowerInvariant(); if (text2 == null) { return; } } s2.Stop(); // Version 3: ToLower with CultureInfo argument. Stopwatch s3 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { string text2 = text.ToLower(c); if (text2 == null) { return; } } s3.Stop(); Console.WriteLine(s1.ElapsedMilliseconds); Console.WriteLine(s2.ElapsedMilliseconds); Console.WriteLine(s3.ElapsedMilliseconds); } } Output 186 ms ToLower 130 ms ToLowerInvariant 154 ms ToLower(CultureInfo)
ToUpper example. ToUpper uppercases all letters in a string. It is useful for processing text input or for when you need to check the string against an already uppercase string.

Example: ToUpper is an instance method on the string type, which means you must have a string variable instance to call it.

Tip: ToUpper works the same as ToLower except it changes lowercase characters to uppercase characters.

CultureInfo: The CultureInfo.Invariant culture class specifies we want the string to be uppercased the same way on all computers.

C# program that uses ToUpper using System; using System.Globalization; class Program { static void Main() { // // Uppercase this mixed case string. // string value1 = "Lowercase string."; string upper1 = value1.ToUpper(); Console.WriteLine(upper1); // // Uppercase this string. // string value2 = "ABC123"; string upper2 = value2.ToUpper(CultureInfo.InvariantCulture); Console.WriteLine(upper2); } } Output LOWERCASE STRING. ABC123
ToLowerInvariant. ToLowerInvariant and ToUpperInvariant affect strings differently than ToLower and ToUpper. The word "invariant" indicates the system's culture has no effect on the result.

Example: This program shows that the invariant methods act upon the characters in the expected way.

And: In some cases, these invariant methods can be different from other methods because they specify the invariant culture.

C# program that uses invariant case methods using System; class Program { static void Main() { // This demonstrates the invariant methods. // ... They act in the expected way. string test1 = "Cat"; Console.WriteLine(test1.ToLowerInvariant()); Console.WriteLine(test1.ToUpperInvariant()); } } Output cat CAT
Notes, invariant. Microsoft states that ToLowerInvariant and ToUpperInvariant are useful only for OS "identifiers" and only affect behavior with specific locales (Turkish).

And: These methods help when you need lowercase or uppercase version of an OS identifier, such as a file name, named pipe, or registry key.

ToLowerInvariant Method: Microsoft Docs

Char: The char type offers a ToLowerInvariant method, which has the same effect for a char.

Notes, continued. Invariant methods usually have the same effect as the default methods. In other words, ToLower is the same in most places to ToLowerInvariant.

Turkish: The documents indicate that these methods will only change behavior with Turkish cultures.

IsLower, IsUpper. If a string is already lowercase, we can simply do nothing. We must first scan for validity. This optimization can help if our data is usually already lowercase.IsUpper, IsLower
A summary. We used ToLower and ToUpper. We also used ToLowerInvariant, which in 2019 appears to perform the fastest in the .NET Framework.
© 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