C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It provides several methods. For example, it has methods for changing the case of strings. We use and then benchmark its ToLower method.
ToLower TextInfo benchmark See "Benchmark" section below for more information. Iterations: 10000000 iterations were tested. TextInfo ToLower: 737 ms [faster] String ToLower: 938 ms
Example. The .NET Framework defines a TextInfo class that encapsulates certain string-level globalization 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.
C# program that uses TextInfo lowercase 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
The program first evaluates the TextInfo virtual property accessors on the InvariantCulture and CurrentCulture properties. Include the System.Globalization namespace or specify the fully qualified name "System.Globalization.CultureInfo".
String ToLower. The String type defines a ToLower method and ToLowerInvariant method that implement the same functionality as the TextInfo members. The ToLower and ToLowerInvariant methods on strings 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. We benchmark the TextInfo ToLower method and the string type's ToLower method. 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();
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. This site has more information on these TextInfo methods.
Summary. You can use the TextInfo class to lowercase strings. We benchmarked the TextInfo code and found that a virtual property accessor can be eliminated by using the TextInfo class and storing it in a variable.
And: There are other TextInfo methods such as ToUpper and ToTitleCase that provide excellent ways to transform string characters.