【问题标题】:Is there a way for me to convert O'reilly to O'Reilly?有没有办法让我将 O'reilly 转换为 O'Reilly?
【发布时间】: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 提到的内容

标签: c# uppercase


【解决方案1】:

你可以试试。

var titlecase = PrintName("o'riley");

调用这个函数

Public static string PrintName(string StrValue)//pass here - o'riley
        {
            if (!string.IsNullOrEmpty(StrValue))
            {
                return Regex.Replace(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(StrValue.ToLower()),
                 "['-](?:.)", m => m.Value.ToUpperInvariant());
            }
            else
            {
                return "Something meaningful message";
            }
        } 

【讨论】:

    【解决方案2】:

    您不能仅使用技术工具来做到这一点。有些非洲名字根本不以大写字母开头。正如您在此实用程序 (http://www.johncardinal.com/tmgutil/capitalizenames.htm) 中所见,最简单的方法是实际维护一个例外列表并将您的姓名与其匹配。

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 1970-01-01
      • 2014-11-06
      • 2017-03-25
      • 2021-09-01
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多