【发布时间】:2019-05-27 15:21:16
【问题描述】:
我想以标题大小写显示名称,并正确转换带连字符的名称(例如 O'Reilly)。
现在,当我使用 ToUpperCase 函数时,我得到“O'reilly”,这不是我想要的。
这是我正在使用的功能:
@functions
{
public static class TextConvert
{
public static string ToTitleCase(string s)
{
s = s.ToLower();
return Regex.Replace(s, @"(^\w)|(\s\w)",b => b.Value.ToUpper());
}
}
}
考虑到像 O'Reilly 这样的案例,我该怎么做?
【问题讨论】:
-
... 会有 MacDuff 和 McGregor 和 ...
-
TitleCase 首先:
var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());然后正则表达式return Regex.Replace(titlecase, "['-](?:.)", m => m.Value.ToUpperInvariant());对于 ` 或 - 不包括 Filburt 提到的内容