The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    cropped.20120723-ESXi5-step-L

    cropped.20120723-ESXi5-step-K

    cropped.20120723-ESXi5-step-J

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,023 other followers

Wordify: .NET/C# – Convert enums to human readable values – Stack Overflow

Posted by jpluimers on 2013/01/03

You’d hope that a method like Wordify with the signature below would be simple right?

public static string Wordify(string pascalCaseString)

Not so.
It uses some RegEx, and I wonder who’d be able to maintain these things: I can’t even assess if this is clever or not so clever RegEx.

    /// <summary>
    /// Add spaces to separate the capitalized words in the string, 
    /// i.e. insert a space before each uppercase letter that is 
    /// either preceded by a lowercase letter or followed by a 
    /// lowercase letter (but not for the first char in string). 
    /// This keeps groups of uppercase letters - e.g. acronyms - together.
    /// </summary>
    /// <param name="pascalCaseString">A string in PascalCase</param>
    /// <returns></returns>
    public static string Wordify(string pascalCaseString)
    {            
        Regex r = new Regex("(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])");
        return r.Replace(pascalCaseString, " ${x}");
    }

–jeroen

via: c# – Convert enums to human readable values – Stack Overflow.

One Response to “Wordify: .NET/C# – Convert enums to human readable values – Stack Overflow”

  1. Paul Hilt said

    I had my fun with some Java RegEx functions (Pattern and Matcher), even wrote a few myself after a while. The testcase for this function would be “IBMTextString” have to brush up on the C# RegEx function to see if this test passes or not.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 1,023 other followers

%d bloggers like this: