stmarti:
rjcox:private static Regex LowThenUpRegex=new Regex(@"(\p{Ll})(\p{Lu})",RegexOptions.None);static string Convert(string input){return LowThenUpRegex.Replace(input,"$1 $2");}
That is it! A 2 line regexp solution
Actually, it's a one line solution, and with no extra spaces either! Let's not waste these spaces and new lines, they are scarce I've heard ;-)
Reducing the number of lines is seldom equivalent to improving the code quality...
Regular expressions are far too complicated and terse in their syntax to make for good code in most cases. They are also limited in their cultural awareness, and are not really suitable for handing human-generated input. Just see how hard it is to get a correct regex for this simple problem. The original code posted as a link, is certainly longer - but at least 10 times as many developers are capable of maintaining that code for various upcoming requirements, perhaps:
- If the string is in camel-case, convert it as it it was Pascal case.
- If the string contains known 2-letters uppercase acronyms, such as ID or IP, handle these.
- Handle the letter classification according to the current system or UI-culture.
I'm not saying you can't do this with regular expressions - I'm saying that fewer developers can, and they will require more time to do it, and even fewer will understand the result.
Write code firstly for other programmers to read, understand, test and maintain. Secondly, when proven by measurements, code specific parts for performance. Never code for elegance or terseness at the expense of clarity.
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager -
http://www.axantum.com[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.