Home > Getting Started > ASP.NET Base Types > Basic String Operations > Changing Case
Getting Started ASP.NET Base Types Basic String Operations
If you write an application that accepts input from a user, you can never be sure what case he or she will use to enter the data. Because the methods that compare strings and characters are case-sensitive, you should convert the case of strings entered by users before comparing them to constant values. You can easily change the case of a string. The following table describes the two available case-changing methods.
Method Name |
Use |
String.ToUpper |
Converts all characters in a string to uppercase. |
String.ToLower |
Converts all characters in a string to lowercase. |
Each method provides an override that accepts a culture.
The String.ToUpper method changes all characters in a string to uppercase. The following code example converts the string "Hello World!" from mixed case to uppercase.
[ VB ]
Dim myString As String = "Hello World!"
Console.WriteLine ( myString.ToUpper ( ) )
[ C# ]
String myString = "Hello World!";
Console.WriteLine ( myString.ToUpper ( ) );
This example displays HELLO WORLD!to the console.
The String.ToLowermethod is similar to the previous method, but instead converts all the characters in a string to lowercase. The following code example converts the string "HELLO WORLD!" to lowercase.
[ VB ]
Dim myString As String = "Hello World!"
Console.WriteLine ( myString.ToLower ( ) )
[ C# ]
String myString = "Hello World!";
Console.WriteLine ( myString.ToLower ( ) );
This example displays hello world! to the console.
Basic String Operations