Converting Strings to Uppercase in C#
The primary method for converting a string to uppercase in C# is by using the ToUpper
method. This method is available on the string
class and returns a new string with all the characters converted to uppercase.
using System;
class Program
{
static void Main()
{
// Input string
string inputString = "Hello, World!";
// Convert to uppercase
string uppercaseString = inputString.ToUpper();
// Output the result
Console.WriteLine("Original String: " + inputString);
Console.WriteLine("Uppercase String: " + uppercaseString);
}
}
Output:
Original String: Hello, World!
Uppercase String: HELLO, WORLD!
In this example, the ToUpper
method is applied to the inputString
, resulting in a new string where all characters are in uppercase. The original string remains unchanged.
Comments (0)