The life of a code monkey

Text

Displaying camel case

Sometimes it is convenient to convert a string that is in camel case (variable name, enum value, method name etc.) to a well formatted string that look nice to an average user. 

The following Regex will find all capital letters that are surrounded by lower-case letters.

(?<=[a-z])(?<Letter>[A-Z])(?=[a-z])

Here is the complete program for testing.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a camel case string");
        string camelCaseString = Console.In.ReadLine() ?? string.Empty;
        var regex = new Regex("(?<=[a-z])(?<Letter>[A-Z])(?=[a-z])");
        Console.WriteLine(regex.Replace(camelCaseString, " ${Letter}"));
        Console.ReadLine();
    }
}
Posted on Thursday, July 7 2011. Tagged with: RegexCCamel Case
1
Notes
  1. financialiphone liked this
  2. mystonyfieldex liked this
  3. certificationex liked this
  4. theresaup liked this
  5. dotnetgeek posted this
Got a question or comment? Ask me.
Previous Next