다음을 통해 공유


Changing Case

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.

ToUpper

The String.ToUpper method changes all characters in a string to uppercase. The following example converts the string "Hello World!" from mixed case to uppercase.

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 preceding example is culture-sensitive by default. To perform a culture-insensitive case change, use an overload of the String.Upper method that allows you to specify the culture to use by supplying a culture parameter. For an example that demonstrates how to use the String.Upper method to perform a culture-insensitive case change, see Performing Culture-Insensitive Case Changes.

ToLower

The String.ToLower method is similar to the previous method, but instead converts all the characters in a string to lowercase. The following example converts the string "Hello World!" to lowercase.

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.

The preceding example is culture-sensitive by default. To perform a culture-insensitive case change, use an overload of the String.Lower method that allows you to specify the culture to use by supplying a culture parameter. For an example that demonstrates how to use the String.Lower method to perform a culture-insensitive case change, see Performing Culture-Insensitive Case Changes.

See Also

Basic String Operations | Performing Culture-Insensitive String Operations