c# - toTitleCase 忽略 C# 中的序数

标签 c# .net regex string

我正在尝试找出一种使用 toTitleCase 来忽略序数的方法。它按照我希望的方式适用于除序数之外的所有字符串(例如,1st、2nd、3rd 变为 1St、2Nd、3Rd)。

如有任何帮助,我们将不胜感激。正则表达式可能是处理这个问题的方法,我只是不确定如何构造这样的正则表达式。

更新:这是我使用的解决方案(使用我在下面的扩展方法中编写的约翰的答案):

public static string ToTitleCaseIgnoreOrdinals(this string text)
{
    string input = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);
    string result = System.Text.RegularExpressions.Regex.Replace(input, "([0-9]st)|([0-9]th)|([0-9]rd)|([0-9]nd)", new System.Text.RegularExpressions.MatchEvaluator((m) => m.Captures[0].Value.ToLower()), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    return result;
}

最佳答案

string input =  System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase("hello there, this is the 1st");
string result = System.Text.RegularExpressions.Regex.Replace(input, "([0-9]st)|([0-9]th)|([0-9]rd)|([0-9]nd)", new System.Text.RegularExpressions.MatchEvaluator((m) =>
{
    return m.Captures[0].Value.ToLower();
}), System.Text.RegularExpressions.RegexOptions.IgnoreCase);

关于c# - toTitleCase 忽略 C# 中的序数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168617/

相关文章:

.net - 尝试读取或写入 protected 内存

c# - 如何在 Entity Framework 中使用扩展方法

c# - 如何在sql server中获取这一列的总和

javascript - 再次运行正则表达式搜索并替换 javascript

c# - 从其他类/范围获取 var

c# - 从继承的 UserControl 获取初始尺寸

c# - 如果 (picturebox1.location =

c# - 什么会导致 Dapper cast 失败?

c# - 从解析的字符串构建多级数组数组/锯齿状数组

java正则表达式不获取不以点结尾的字符串