Title Case in C#
ByHere a quick snippet to convert text to Title Case in C# that uses the TextInfo class. Note that strings with all caps will not be converted, so convert to lower case first.
// Defines the string with mixed casing.
string myString = "this iS a StrIng with MIXED CaSe";
// Create a TextInfo based on the "en-US" culture.
TextInfo tInfo = new CultureInfo("en-US", false).TextInfo;
// Changes a string to titlecase.
// Note the ToLower(), needed to convert words in all caps like MIXED
Console.WriteLine("\"{0}\" to titlecase: {1}", myString, tInfo.ToTitleCase(myString.ToLower()));
//Result: This Is A String With Mixed Case
Console.ReadLine();
// Defines the string with mixed casing.
string myString = “this iS a StrIng with MIXED CaSe”;
// Create a TextInfo based on the “en-US” culture.
TextInfo tInfo = new CultureInfo(“en-US”, false).TextInfo;
// Changes a string to titlecase.
// Note the ToLower(), needed to convert words in all caps like MIXED
Console.WriteLine(“\”{0}\” to titlecase: {1}”, myString, tInfo.ToTitleCase(myString.ToLower()));
//Result: This Is A String With Mixed Case
Console.ReadLine();
Related posts:

