我在 Razor 中使用 MVC 3。我不知道如何编写字符串扩展正则表达式来执行此操作:
This_is_some_text
显示:
This is some text
我为下拉列表设置了一些枚举,因此它们出现了(显然我不能创建带空格的枚举):
public enum MyProperty
{
This_is_some_text,
This_is_some_other_text
}
如果我这样做的话,我只是无法弄清楚正则表达式是否会做我想做的事:
public static string EnumToDisplay(this string str)
{
return Regex.Replace(str, "[What is the regex I should use?]");
}
作为对我的奖励,我还想添加一个句点“。”到枚举的末尾。这样做的原因是我有强制症,我的枚举采用短句的形式。 :)
谢谢!
请您参考如下方法:
为什么不使用 String.Replace()
为了这? RegEx 似乎有点矫枉过正。
public static string EnumToDisplay(this string str)
{
return str.Replace('_', ' ') + ".";
}