Search code examples
c#stringcapitalize

How do I capitalize first letter of first name and last name in C#?


Is there an easy way to capitalize the first letter of a string and lower the rest of it? Is there a built in method or do I need to make my own?


Solution

  • TextInfo.ToTitleCase() capitalizes the first character in each token of a string.
    If there is no need to maintain Acronym Uppercasing, then you should include ToLower().

    string s = "JOHN DOE";
    s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());
    // Produces "John Doe"
    

    If CurrentCulture is unavailable, use:

    string s = "JOHN DOE";
    s = new System.Globalization.CultureInfo("en-US", false).TextInfo.ToTitleCase(s.ToLower());
    

    See the MSDN Link for a detailed description.