This tutorial will teach us how to convert a string into an upper character string with an example.
Uppercase method in C#
In C#, The .ToUpper() method returns a new string with all the characters converted to uppercase.
Same as .ToLower() is used for lowercase.
Syntax:
somestring.ToUpper()
This method will not change the input string.
.ToUpper()
Numbers and symbols are not changed. This method does not modify the original string, but instead, it creates a new string that contains the uppercase version of the original string.
String Initialization:
- Here, we declare a string variable named a and assign it the value “programmer: a machine that turns coffee into code.”.
- The text within the double quotes represents the content of the string.
Console Output:
Console.WriteLine(a);
- This line prints the value of the string variable a to the console.
- When executed, it will display: “programmer: a machine that turns coffee into code.”
Uppercase Transformation:
Console.WriteLine(a.ToUpper());
- The ToUpper() method is called on the string a.
- This method converts all characters in the string to uppercase.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace uppercase { class Program { static void Main(string[] args) { string a = "programmer: a machine that turns coffee into code."; Console.WriteLine(a); Console.WriteLine(a.ToUpper()); Console.ReadLine(); } } }
Output:
The first line displays the original string, and the second line shows the uppercase version of the same string.