I have enums that I want to display on a form and I wanted to make them look a little better on the screen.  The constants defined in the enumerator list are defined using Camel case.  Like “firstName” or  “mobileNumber”.  Most of the values could be displayed by converting the Camel cased string to words.

Using some bits and pieces from Stack Overflow, I wrote a string extension class to do the conversion.  The first thing it does is to convert the enum to a string, then force the first letter to uppcase case, then finally split the text into words, with each uppercase letter designating a new word.

public static class StringHelper
{
    public static string FirstCharToUpper(this string input)
    {
        if (String.IsNullOrEmpty(input))
            return input;

        return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
    }

    public static string CamelCaseToWords(this string input)
    {
        return Regex.Replace(input.FirstCharToUpper(), "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ");
    }
}

I posted an example on DotNetFiddle, which is basically the C#/VB.Net/F# equivalent of JSFiddle, a great tool for quickly trying out some code.  The sharing feature is nice.  I can embed a runnable copy of code (as shown below) or I can work with another developer using their TogetherJS integration.

The cool thing about the sample code above is that you can edit it in-place and try some other values.  Replace “thisIsCamelCase” with “MooseAndSquirrel” and you should see the results immediately.