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.