Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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 two case-changing methods. Each method provides an overload that accepts a culture.

Method name

Use

String.ToUpper

Converts all characters in a string to uppercase.

String.ToLower

Converts all characters in a string to lowercase.

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.

string properString = "Hello World!";
Console.WriteLine(properString.ToUpper());
// This example displays the following output: 
//       HELLO WORLD!

The preceding example is culture-sensitive by default; it applies the casing conventions of the current culture. To perform a culture-insensitive case change or to apply the casing conventions of a particular culture, use the String.ToUpper(CultureInfo) method overload and supply a value of CultureInfo.InvariantCulture or a System.Globalization.CultureInfo object that represents the specified culture to the culture parameter. For an example that demonstrates how to use the ToUpper method to perform a culture-insensitive case change, see Performing Culture-Insensitive Case Changes.

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.

string properString = "Hello World!";
Console.WriteLine(properString.ToLower());
// This example displays the following output: 
//       hello world!

The preceding example is culture-sensitive by default; it applies the casing conventions of the current culture. To perform a culture-insensitive case change or to apply the casing conventions of a particular culture, use the String.ToLower(CultureInfo) method overload and supply a value of CultureInfo.InvariantCulture or a System.Globalization.CultureInfo object that represents the specified culture to the culture parameter. For an example that demonstrates how to use the ToLower(CultureInfo) method to perform a culture-insensitive case change, see Performing Culture-Insensitive Case Changes.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.