ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Enum Name To Display Name

Convert an CamelCase enum name to displayable string

Source

private static Regex UpperCamelCaseRegex = new Regex(@"(?<!^)((?<!\d)\d|(?(?<=[A-Z])[A-Z](?=[a-z])|[A-Z]))", RegexOptions.Compiled);

public static string AsUpperCamelCaseName(this Enum e)
{
    return UpperCamelCaseRegex.Replace(e.ToString(), " $1");
}

Example

foreach(HttpStatusCode c in Enum.GetValues(typeof(HttpStatusCode)))
{
    Console.WriteLine("Name:{0} - {1}", c, c.AsUpperCamelCaseName());
}

Author: AdamTha

Submitted on: 3 feb. 2015

Language: C#

Type: System.Enum

Views: 7495