views:

1512

answers:

1

I would like to take a pascal-cased string like "CountOfWidgets" and convert it into something more user-friendly like "Count of Widgets" in C#. Multiple adjacent uppercase characters should be left intact. What is the most efficient way to do this?

NOTE: Duplicate of http://stackoverflow.com/questions/155303/net-how-can-you-split-a-caps-delimited-string-into-an-array

+3  A: 

Don't know about efficient but at least it's terse:

Regex r = new Regex("([A-Z]+[a-z]+)");
string result = r.Replace("CountOfWidgets", m => (m.Value.Length > 3 ? m.Value : m.Value.ToLower()) + " ");
Cristian Libardo