This article demonstrates how to convert a string to uppercase, lowercase, or title (or proper) case.
Although you can use methods of the String class to convert a string to uppercase or lowercase, you must use a method in the TextInfo class of the System.Globalization namespace to convert a string to title case. The String class does not include a built-in method to convert a string to title case.
Convert String Using the String Class
This section describes how to use the methods of the String class to convert a string to uppercase and lowercase.
Convert a String to Uppercase
The String class has a static method named ToUpper. You can use this method to convert a string to uppercase.
For example:
string lower = "converted from lowercase";
Console.WriteLine(lower.ToUpper());
Convert a String to Lowercase
The ToLower method is the complement of the ToUpper method. ToLower converts a string to lowercase.
For example:
string upper = "CONVERTED FROM UPPERCASE";
Console.WriteLine(upper.ToLower());
Convert String Using the TextInfo Class
This section describes how to use the TextInfo class to convert a string to title case.
Convert a String to Title Case
The String class does not include a method that converts a string to title case. The ToTitleCase method resides in the TextInfo class, which is a member of the System.Globalization namespace. Unlike the ToUpper and ToLower methods of the String class, the ToTitleCase method is not a static method and requires an instance of the class.
When you use the TextInfo class, you must specify cultural information. In most situations, you can default to the culture that is currently in use. Culture information is a property of the thread on which the code is running. To obtain the culture information, you must gain access to the current thread and retrieve the CurrentCulture property from that thread. Once you accomplish this, you can create the TextInfo object.
For example:
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
The TextInfo class also includes the ToUpper and ToLower methods. Use these methods of TextInfo if you need to specify culture options.
Console.WriteLine(textInfo.ToTitleCase(title));
Console.WriteLine(textInfo.ToLower(title));
Console.WriteLine(textInfo.ToUpper(title));
Note on the InvariantCulture Property
When you use the Globalization namespace to convert data, if you store the converted data instead of displaying it for the user, you may want to use the InvariantCulture property of CultureInfo class.
InvariantCulture is neither a neutral nor a specific culture; it is a culture that is culture-insensitive. If you use InvariantCulture when you store data, the data is stored in a consistent manner, regardless of any specific user or cultural system settings that may be in effect. For more information, refer to the References section.
The preceding code sample uses the CultureInfo properties of the current thread:
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
To use InvariantCulture in the same scenario, use the following code:
CultureInfo cultureInfo = CultureInfo.InvariantCulture;
for more : http://support.microsoft.com/kb/312890
We collect videos & images from online sites like youtube and some other websites which provide them. And most of the News is also gathered from other online websites. Any material downloaded or otherwise obtained through the use of the service is done at your own discretion and risk and that you will be solely responsible for any damage to your computer system or loss of data that results from the download of any such material.
If you feel that the content on the site is illegal or Privacy please contact us at srinuchalla@gmail.com and such videos, images or any Content will be removed with immediate effect.
No comments:
Post a Comment