C# TextInfo Examples

This C# article shows the TextInfo type. It benchmarks 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 = "Sam Allen"; 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.

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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Dot Net Perls