The Wiert Corner – irregular stream of stuff

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

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

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

    Join 719 other subscribers

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?

[sourcecode language=”csharp”]
public static string Wordify(string pascalCaseString)
[/sourcecode]

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.

[sourcecode language=”csharp”]
/// <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}");
}
[/sourcecode]

–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's avatar

    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 Comment