Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a Pascal-case string into logical set of words [duplicate]

Tags:

string

c#

regex

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 .NET - How can you split a "caps" delimited string into an array?

like image 749
JoshL Avatar asked Nov 14 '08 23:11

JoshL


1 Answers

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()) + " ");
like image 59
Cristian Libardo Avatar answered Oct 25 '22 20:10

Cristian Libardo