Insert spaces between words on a camel-cased token [duplicate]

See: .NET - How can you split a "caps" delimited string into an array?

Especially:

Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")

Here's an extension method that I have used extensively for this kind of thing

public static string SplitCamelCase( this string str )
{
    return Regex.Replace( 
        Regex.Replace( 
            str, 
            @"(\P{Ll})(\P{Ll}\p{Ll})", 
            "$1 $2" 
        ), 
        @"(\p{Ll})(\P{Ll})", 
        "$1 $2" 
    );
}

It also handles strings like IBMMakeStuffAndSellIt, converting it to IBM Make Stuff And Sell It (IIRC).

Syntax explanation (credit):

{Ll} is Unicode Character Category "Letter lowercase" (as opposed to {Lu} "Letter uppercase"). P is a negative match, while p is a positive match, so \P{Ll} is literally "Not lowercase" and p{Ll} is "Lowercase".
So this regex splits on two patterns. 1: "Uppercase, Uppercase, Lowercase" (which would match the MMa in IBMMake and result in IBM Make), and 2. "Lowercase, Uppercase" (which would match on the eS in MakeStuff). That covers all camelcase breakpoints.
TIP: Replace space with hyphen and call ToLower to produce HTML5 data attribute names.


Simplest Way:

var res = Regex.Replace("FirstName", "([A-Z])", " $1").Trim();

You can use a regular expression:

Match    ([^^])([A-Z])
Replace  $1 $2

In code:

String output = System.Text.RegularExpressions.Regex.Replace(
                  input,
                  "([^^])([A-Z])",
                  "$1 $2"
                );