Converting Pascal case to sentences using regular expression
April 29, 2009
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