How to convert a text content into Title Case in C#?

Posted by SheoNarayan under C# category on | Views : 9127
To convert a text content into Title Case, below code snippet can be used.

     string stringToConvert = "THIS is a sample String to convert to titLe cAse";
System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Globalization.TextInfo txtInfo = cultureInfo.TextInfo;
Response.Write("Original string: " + stringToConvert + "<br />");
Response.Write("Title case string: " + txtInfo.ToTitleCase(stringToConvert));

The output of the above code will be "THIS Is A Sample String To Convert To Title Case "

Note: Words with upper case will not be converted, in this case you should convert all text to lower case and then use ToTitleCase function to convert to Title case.


Comments or Responses

Login to post response