更新时间:2023-02-18 21:08:14
根据你打算做资本我会用天真的方法去多久。 。你可能用正则表达式做,但事实上,你不希望某些字大写,使这有点棘手
Depending on how often you plan on doing the capitalization I'd go with the naive approach. You could possibly do it with a regular expression, but the fact that you don't want certain words capitalized makes that a little trickier.
编辑:
您可以使用正则表达式两遍做
You can do it with two passes using regexes
var result = Regex.Replace("of mice and men isn't By CNN", @"\b(\w)", m => m.Value.ToUpper());
result = Regex.Replace(result, @"(\s(of|in|by|and)|\'[st])\b", m => m.Value.ToLower(), RegexOptions.IgnoreCase);
这输出小鼠和人通过CNN $ c不是$ C>。
第一个表达式大写一个字边界上每一个字母,第二个downcases任何文字匹配由空格包围名单。
The first expression capitalizes every letter on a word boundary and the second one downcases any words matching the list that are surrounded by whitespace.
的缺点这种方法是,你正在使用regexs(现在你有两个问题),你需要不断的排除单词列表保持最新状态。我正则表达式福不够好,能够做到这一点的一种表现,但也可能是可行的。
The downsides to this approach is that you're using regexs (now you have two problems) and you'll need to keep that list of excluded words up to date. My regex-fu isn't good enough to be able to do it in one expression, but it might be possible.