C# TextInfo ExamplesUse the TextInfo type and benchmark the ToLower method on TextInfo.
TextInfo. This type is found in System.Globalization—it provides several C# methods. For example, it has methods for changing the case of strings.
Method intro. 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.
An example. In .NET we have 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".
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; // Example strings. const string value1 = "Yellow Bird"; const string value2 = "YELLOW BIRD 012345"; const string value3 = "yellow bird"; // 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); } }
yellow bird yellow bird 012345 yellow bird
Benchmark, TextInfo ToLower. The benchmark shows that you can improve performance by using a CultureInfo.CurrentCulture.TextInfo variable and calling the instance ToLower method on it.
Version 1 This version of the code calls the ToLower instance method on the TextInfo variable.
Version 2 Here we call the ToLower method from the string class. The output, "graycat" should be the same in both.
Result The code that uses the ToLower method on a TextInfo instance is consistently faster.
using System; using System.Diagnostics; using System.Globalization; class Program { const int _max = 1000000; static void Main() { TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo; var s1 = Stopwatch.StartNew(); // Version 1: use TextInfo ToLower. for (int i = 0; i < _max; i++) { string lower = textInfo.ToLower("GRAYCAT"); } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use String method. for (int i = 0; i < _max; i++) { string lower = "GRAYCAT".ToLower(); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }
29.88 ns TextInfo ToLower 40.72 ns String ToLower
A review. You can use the TextInfo class to lowercase strings. TextInfo methods (such as ToUpper and ToTitleCase) provide excellent ways to transform string characters.
© 2007-2022 sam allen.
see site info on the changelog.