Convert a string to proper case (title case) in C#

[proper case]

In proper case (or title case) the first letter of each word is capitalized. This is actually pretty easy once you know where to find the method that converts to proper case.

The example uses the following code to convert the string in the variable txt into proper case.

// Convert to proper case.
CultureInfo culture_info = Thread.CurrentThread.CurrentCulture;
TextInfo text_info = culture_info.TextInfo;
txt = text_info.ToTitleCase(txt);

The code creates a CultureInfo object representing the program’s current culture. (This uses the System.Globalization and System.Threading namespaces.) It gets the culture’s TextInfo object and calls its ToTitleCase method to convert the text to proper case.

Note that this method isn’t perfect. For example, it will keep a string in all caps such as NASA in all caps but it won’t preserve odd mixed case examples such as VBScript. It also doesn’t consider an apostrophe to be the start of a new word so, for example, it would capitalize the name O’Dell as O’dell. Those are odd cases, however, and I wouldn’t expect it to handle every case perfectly.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in strings and tagged , , , , , , , , , , , . Bookmark the permalink.

3 Responses to Convert a string to proper case (title case) in C#

  1. Dwight Kurt Schrute III says:

    Bless you Sir,
    This is by far the simplest solution ive seen.

    Many Thanks.

  2. Krish Patel says:
    /* A java program to convert an entered string into 5 different case that are
     * Upper case,( UPPER CASE)
     * Lower case,(LOWER CASE)
     * Sentence case,(Sentence case)
     * Title case,(Title Case)
     * Toggle case.(tOGGLE CASE)
    import java.util.*;
    public class Assignment_17
        public static void main()
            Scanner in = new Scanner(;
            System.out.println("Enter a text.");
            String str = in.nextLine().trim(),sen="",titl="",tog="";
            System.out.println("The Entered text in the 5 different cases :- ");
            System.out.println("Upper Case :- "+str.toUpperCase());
            System.out.println("Lower Case :- "+str.toLowerCase());
            for(int i=1;i<str.length();i++)
             if((((str.charAt(i-1))==' ')&&(str.charAt(i-2))=='.')&&((Character.isLetter(str.charAt(i)))))
             if(str.charAt(i-1)==' ')
            System.out.println("Sentence Case :- "+sen);
            System.out.println("Title Case :- "+titl);
            System.out.println("Toggle Case :- "+tog);
  3. JuanPablo Jofre says:

    Here is some C# code to cover some corner cases when converting to proper casing

    public static readonly char[] WhiteSpaceChars = new char[]{
        // See this link to get understanding of what's considered a white-space
        // characters in .Net 5.0.
        , '\u000A'
        , '\u000B'
        , '\u000C'
        , '\u000D'
        , '\u0085'
        , '\u2028'
        , '\u2029'
        , '\u0020'
        , '\u00A0'
        , '\u1680'
        , '\u2000'
        , '\u2001'
        , '\u2002'
        , '\u2003'
        , '\u2004'
        , '\u2005'
        , '\u2006'
        , '\u2007'
        , '\u2008'
        , '\u2009'
        , '\u200A'
        , '\u202F'
        , '\u205F'
        , '\u3000'
    public static string ToProper(string text)
        if (string.IsNullOrWhiteSpace(text))
            return text;
        text = text.Trim();
        text = Regex.Replace(text, $"({string.Join('|', WhiteSpaceChars)})+", " ", RegexOptions.Singleline);
        CultureInfo culture_info = CultureInfo.InvariantCulture;
        TextInfo text_info = culture_info.TextInfo;
        text = text_info.ToTitleCase(text);
        if (!text.Contains('\''))
            return text;
        StringBuilder sb = new StringBuilder(text);
        // Capitalize letter after the apostrophe
        int i = sb.ToString().IndexOf('\'');
        while (i != -1 && i+1 < sb.Length)
            sb[i+1] = Char.ToUpper(sb[i+1], CultureInfo.InvariantCulture);
            i = sb.ToString().IndexOf('\'', i+1);
        return sb.ToString();

Comments are closed.