Fake binary trees
... rants, ramblings and occasional good idea ...

Converting Pascal case to sentences using regular expression

Here's another reminder to myself: a nifty one-liner regex which transforms Pascal/camel case string into a sentence. I always write it from scratch so I decided to put it here. It might also be a good idea to write it as an extension method for string class.

static string PascalCaseToSentence(string input)
{
   return Regex.Replace(input, ".[A-Z]", m => m.ToString()[0] + " " + char.ToLower(m.ToString()[1]));
}

Following line

Console.Out.WriteLine(PascalCaseToSentence("MyVeryLongSentence"));

will result in:

My very long sentence
Posted at 22:56 on April 29, 2009
Categories: .NET   E-mail | del.icio.us | Permalink | Comments (0) | Post RSSRSS comment feed