C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It is useful for processing text input or for when you need to check the string against an already uppercase string. It has no effect on non-lowercase characters.
Example. ToUpper is an instance method on the string type, which means you must have a string variable instance to call it. ToUpper works the same as ToLower except it changes lowercase characters to uppercase characters.
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 = "R2D2"; string upper2 = value2.ToUpper(CultureInfo.InvariantCulture); Console.WriteLine(upper2); } } Output (Second string is not changed.) LOWERCASE STRING. R2D2
ToUpper converts all lowercase characters to uppercase characters, ignoring non-lowercase characters such as the uppercase L and the period. The second part tries to uppercase a string that is already uppercased. Nothing is changed.
Next, the CultureInfo.Invariant culture class specifies that you want the string to be uppercased the same way on all computers that might run your program. It is useful for larger programs.
Tip: If you need to uppercase the first letter, consider ToCharArray and char.ToUpper, not the ToUpper string instance method.
Summary. In this example, we looked at the ToUpper instance method on strings. We saw that this method uppercases all letters in strings. And it leaves non-lowercase letters completely unchanged.
Also: We touched on the invariant culture method and noted other uppercase solutions.