I am using MVC 3 with Razor. I can’t figure out how to code a string extension regex to take this:
This_is_some_text
to display:
This is some text
I set up some enums for a drop down list, so they appear thusly (obviously I can’t create an enum with a space):
public enum MyProperty
{
This_is_some_text,
This_is_some_other_text
}
I just cannot figure out the regex is to do what I want if I am doing this:
public static string EnumToDisplay(this string str)
{
return Regex.Replace(str, "[What is the regex I should use?]");
}
As a bonus for me I would also like to add a period “.” to the end of the enum. The reason for this is that I have OCD and my enums are taking the form of short sentences. 🙂
Thanks!
Why do you want to use regular expressions? A very wise man once said, I quote:
How about using the
[Display]attribute which is kinda designed exactly for this purpose:and then writing a custom Html helper:
So now assuming you have a view model:
and a controller:
you could use the custom helper we just wrote in the view to display a user friendly text that we might have associated to an ugly enum. Hey, now you could even have this working with globalization and multiple languages using resources: