Use this regex (I forgot from which stackoverflow answer I sourced it, will search it now):
public static string ToLowercaseNamingConvention(this string s, bool toLowercase)
{
if (toLowercase)
{
var r = new Regex(@"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
return r.Replace(s, "_").ToLower();
}
else
return s;
}
I use it in this project: http://www.ienablemuch.com/2010/12/intelligent-brownfield-mapping-system.html
[EDIT]
I found it now: How do I convert CamelCase into human-readable names in Java?
Nicely split "TodayILiveInTheUSAWithSimon", no space on front of " Today":
using System;
using System.Text.RegularExpressions;
namespace TestSplit
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
var r = new Regex(@"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
string s = "TodayILiveInTheUSAWithSimon";
Console.WriteLine( "YYY{0}ZZZ", r.Replace(s, " "));
}
}
}
Output:
YYYToday I Live In The USA With SimonZZZ