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

前端 未结 9 1137
眼角桃花
眼角桃花 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:06

    Use regular expressions for this looks much cleaner:

    string s = "the quick brown fox jumps over the lazy dog";
    s = Regex.Replace(s, @"(^\w)|(\s\w)", m => m.Value.ToUpper());
    

提交回复
热议问题