Convert string to Title Case/Sentence Case in C# - Thea Burger's Blog

Thea Burger's Blog

Wouldn't you like to know...

News

Photo's!!!

About me

I'm Reading: General Blogs

I'm Reading: Technical Blogs

Convert string to Title Case/Sentence Case in C#

The ToTitleCase method resides in the TextInfo class which is a member of the System.Globalization namespace. The ToTitleCase method is not static and needs an instance of the class.
To gain access to the TextInfo class you first have to specify cultural information which you can get from the CurrentCulture property of the thread the code is running on.

So:

using System.Globalization;
using System.Threading;

//Create CultureInfo and TextInfo classes to use ToTitleCase method
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

oClient.ClientName = textInfo.ToTitleCase(txtClientName.Text);

Ps: Title Case converts every first letter of a word in a sentence to upper case, and the rest to lower case.

Posted: May 28 2004, 11:25 AM by Thea Burger | with 10 comment(s) |
Filed under:

Comments

senkwe said:

*sigh* I hate it when this happens. You have no idea how many times I've written the two lines of code necessary to do exactly this. Thanks Thea!
# May 28, 2004 2:26 PM

Pieter said:

Hey,
Kyk waar kom ek uit.
Thanks thea!
# September 30, 2004 6:50 PM

Fabian said:

i found that not to be too reliable, i prefer using regular expressions.

Regex r = new Regex(@"\b(\w)(\w+)?\b",RegexOptions.Multiline | RegexOptions.IgnoreCase);
MatchEvaluator ME = new MatchEvaluator(MD.ReplaceM);

m_string = m_string.ToLower();
m_string = r.Replace(m_string,ME);


private class MatchDelegate
{

/// <summary>
/// Replace the words with there appriotiriate casing
/// </summary>
/// <param name="m">regular expression match class</param>
/// <returns></returns>
public string ReplaceM(Match m)
{
string FirstLetter;
string Word;
Word = m.Groups[2].Value;
FirstLetter=m.Groups[1].Value.ToUpper();
return FirstLetter + Word;

}
}
# March 17, 2005 1:09 PM

NILSIHLEN said:

I have the same experience as FABIAN, the ToTitleCase does not always work. I'm now using FABIANs grate example and the code is much more robust now.

# April 16, 2005 12:48 PM

Michael said:

The ToTitleCase is not going to be reliable in all cases if the codes creates a TextInfo based on the current Windows culture settings! (...Thread.CurrentThread.CurrentCulture)

Rather, create TextInfo based on the "en-US" culture:
TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;

Then you can still get your Title cased strings with 2 lines of code :)
# June 22, 2005 12:07 PM

Bob the Builder said:

Erm, use your loaf and you can get it in one line

public static string ProperCase(string TextToFormat)
{
return new CultureInfo("en").TextInfo.ToTitleCase(TextToFormat.ToLower());
}

ToTitleCase doesnt seem to like inputs in upper case, hence the ToLower() before we start.
# July 25, 2005 5:42 PM

Pieter said:

Kan jy nou meer, hier kom ek alweer hier uit. :D
Weereens dankie!
# November 10, 2005 4:55 AM

Mathieu said:

it's too simple... on button1_Click

string s=textBox1.Text.ToLower();
string d=s[0].ToString().ToUpper();

for(int i=1;i<s.Length;i++)
if(s[i-1]==' ')
d+=s[i].ToString().ToUpper();
else
d+=s[i].ToString();

textBox1.Text=d;
# December 13, 2005 9:19 PM

david said:

Thank You! Very nice snippet!
# February 25, 2006 2:56 AM

It’s in the Framework, Dummy! | Jason Kemp .ca said:

Pingback from  It&#8217;s in the Framework, Dummy! | Jason Kemp .ca

# April 25, 2008 6:23 PM