How to capitalize the first character of each word, or the first character of a whole string, with C#?

前端 未结 9 1163
眼角桃花
眼角桃花 2020-11-29 05:27

I could write my own algorithm to do it, but I feel there should be the equivalent to ruby\'s humanize in C#.

I googled it but only found ways to humanize dates.

9条回答
  •  余生分开走
    2020-11-29 06:27

    There is another elegant solution :

    Define the function ToTitleCase in an static class of your projet

    using System.Globalization;
    
    public static string ToTitleCase(this string title)
    {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title.ToLower()); 
    }
    

    And then use it like a string extension anywhere on your project:

    "have a good day !".ToTitleCase() // "Have A Good Day !"
    

提交回复
热议问题