Capitalizing words in a string using c#

前端 未结 10 1337
攒了一身酷
攒了一身酷 2020-12-16 11:22

I need to take a string, and capitalize words in it. Certain words (\"in\", \"at\", etc.), are not capitalized and are changed to lower case if encountered. The first word s

10条回答
  •  太阳男子
    2020-12-16 11:51

    A slight improvement on jonnii's answer:

    var result = Regex.Replace(s.Trim(), @"\b(\w)", m => m.Value.ToUpper());
            result = Regex.Replace(result, @"\s(of|in|by|and)\s", m => m.Value.ToLower(), RegexOptions.IgnoreCase);
            result = result.Replace("'S", "'s");
    

提交回复
热议问题