Monday, February 27, 2012

Upper case, Lower case, Proper Case or Title Case in C#


Convert a string to Uppercase

String Sub_Code = txtcode.Text.ToUpper();

Convert a string to Lowercase

String Sub_Code = txtcode.Text.ToLower();

Note : ToLower and ToUpper are methods of string class.

Convert a string to ProperCase or TitleCase
The String class does not include any method that converts a string to title case. To convert  a string to title case we use ToTitleCase method which 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 we use the TextInfo class, we must specify cultural information. CultureInfo class represents information about a specific culture including the names of the culture, the writing system, and the calendar used, as well as access to culture-specific objects that provide information for common operations, such as formatting dates and sorting strings.

Example :
CultureInfo cultureinfo = CultureInfo.InvariantCulture;
TextInfo textinfo = cultureinfo.TextInfo;

string Sub_Name =textinfo.ToTitleCase(txtname.Text);

 

No comments:

Post a Comment