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# TextInfo Examples

Use the TextInfo type and benchmark the ToLower method on TextInfo.
TextInfo is found in System.Globalization. It provides several methods. For example, it has methods for changing the case of strings.ToTitleCase
An example. The .NET Framework has a TextInfo class that encapsulates certain string-level methods. The TextInfo class provides a ToLower method, a ToTitleCase method, and a ToUpper method.

Next: This example shows the ToLower method, but the other methods can be used in the same way. It lowercases constant string data.

First: The program first evaluates the TextInfo virtual property accessors on the InvariantCulture and CurrentCulture properties.

Tip: Include the System.Globalization namespace or specify the fully qualified name "System.Globalization.CultureInfo".

C# program that uses TextInfo using System; using System.Globalization; class Program { static void Main() { // // Access TextInfo virtual property accessors. // TextInfo textInfo1 = CultureInfo.InvariantCulture.TextInfo; TextInfo textInfo2 = CultureInfo.CurrentCulture.TextInfo; // (FAST) // // Input mixed-cased strings. // const string value1 = "thedeveloperblog.com"; const string value2 = "SAM ALLEN 012345"; const string value3 = "sam allen"; // // Use the ToLower method on the TextInfo variables. // string lower1 = textInfo1.ToLower(value1); string lower2 = textInfo1.ToLower(value2); string lower3 = textInfo2.ToLower(value3); // // Write lowercased strings to the screen. // Console.WriteLine(lower1); Console.WriteLine(lower2); Console.WriteLine(lower3); } } Output sam allen sam allen 012345 sam allen
String, ToLower. The String type defines ToLower and ToLowerInvariant methods that implement the same functionality as the TextInfo members. These call into the TextInfo virtual property.

And: For this reason, they always carry the overhead of this virtual property access.

Thus: The string type methods have no difference in result values but are slower in some cases.

ToLower
Benchmark, TextInfo ToLower. The benchmark shows that you can improve performance by using the CultureInfo.CurrentCulture.TextInfo variable and calling the instance ToLower method on it.

Note: This is possible because you can hoist the virtual property access for TextInfo outside of a tight loop.

Note 2: This optimization is only useful when you are calling ToLower many times, in an important method.

Benchmark TextInfo used: C# TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo; Statements tested in separate loops: C# string lower = textInfo.ToLower("SAMDOTNETPERLS"); string lower = "SAMDOTNETPERLS".ToLower(); ToLower TextInfo benchmark Iterations: 10000000 iterations were tested. TextInfo ToLower: 737 ms [faster] String ToLower: 938 ms
Methods. You can call the ToTitleCase method to uppercase the first letter in each word in a string. You can also call the ToUpper method to uppercase all letters in the string.
A review. You can use the TextInfo class to lowercase strings. We benchmarked the TextInfo code. A virtual property accessor can be eliminated with a local variable.
More details. There are other TextInfo methods such as ToUpper and ToTitleCase that provide excellent ways to transform string characters.
© 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