Convert A String To Uppercase In C#
Code snippet for how to Convert A String To Uppercase In C# with sample and detail explanation
Manipulating strings is a vital aspect of programming, and in C#, there are numerous techniques to alter a string’s case. This article will walk you through one of the simplest methods to convert a string to uppercase in C#.
Code snippet for String to Uppercase Conversion
Here is a simple code snippet to convert a string to uppercase in C#.
string myString = "Hello Coding World";
myString = myString.ToUpper();
Console.WriteLine(myString); // Outputs "HELLO CODING WORLD"
Code Explanation for String to Uppercase Conversion
Let’s break it down and understand how this piece of code works, line by line:
string myString = "Hello Coding World";
In this line of code, we define a string variable named myString
and assign it the value Hello Coding World
.
myString = myString.ToUpper();
The ToUpper()
method is an inbuilt function in C# that converts all string characters to uppercase. So, after this line of code, myString
would have been converted from Hello Coding World
to HELLO CODING WORLD
.
Console.WriteLine(myString);
Finally, we print out myString
, allowing the output HELLO CODING WORLD
to appear in the console.
That’s it! With these simple steps you can convert any string to uppercase in C#. You should note that the ToUpper()
method doesn’t modify the original string. Instead, it creates a new string where all characters are in uppercase, and the original string remains unchanged. Therefore, you must assign the result of the ToUpper()
operation to a string variable if you want to keep the uppercase version around for later use.