Regex to convert "camelCase" into "Title Case"

  • Thread starter Thread starter Greg Collins [InfoPath MVP]
  • Start date Start date
G

Greg Collins [InfoPath MVP]

I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with spaces).

Thanx!
 
Use RegexEx.Replace(string, MatchEvaluator) to do this. The MatchEvaluator
is a delegate method that changes the value of each Match in the string to
whatever you define.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Greg Collins [InfoPath MVP]" <Greg.Collins_AT_InfoPathDev.com> wrote in
message I couldn't find anything in my searches... I'm wondering if there's a Regex
(with or without additional C# code) that can convert a either
"lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with
spaces).

Thanx!
 
To convert from UpperCamelCase to Title Case, use this line :
Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");

To convert from both lowerCamelCase and UpperCamelCase to Title Case, use
MatchEvaluator :
public string toTitleCase(Match m) {
char c=m.Captures[0].Value[0];
return ((c>='a')&&(c<='z'))?Char.ToUpper(c).ToString():" "+c;
}
and change a little your regex with this line :
Regex.Replace("UpperCamelCase or lowerCamelCase",@"(\b[a-z]|\B[A-Z])",new
MatchEvaluator(toTitleCase));

Hope it helps,

Ludovic SOEUR.


"Greg Collins [InfoPath MVP]" <Greg.Collins_AT_InfoPathDev.com> a écrit dans
le message de I couldn't find anything in my searches... I'm wondering if there's a Regex
(with or without additional C# code) that can convert a either
"lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with
spaces).

Thanx!
 
Thanks. I knew that had to be something fairly simple.

So which is faster -- to use the Match Evaluator, or to just blindly convert the first character ToUpper() before running the more simple Regex?

public string CamelToTitleCase(string Text)
{
Text = Text.Substring(0, 1).ToUpper() + Text.Substring(1);
return Regex.Replace(Text, @"(\B[A-Z])", @" $1");
}


--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com



To convert from UpperCamelCase to Title Case, use this line :
Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");

To convert from both lowerCamelCase and UpperCamelCase to Title Case, use
MatchEvaluator :
public string toTitleCase(Match m) {
char c=m.Captures[0].Value[0];
return ((c>='a')&&(c<='z'))?Char.ToUpper(c).ToString():" "+c;
}
and change a little your regex with this line :
Regex.Replace("UpperCamelCase or lowerCamelCase",@"(\b[a-z]|\B[A-Z])",new
MatchEvaluator(toTitleCase));

Hope it helps,

Ludovic SOEUR.


"Greg Collins [InfoPath MVP]" <Greg.Collins_AT_InfoPathDev.com> a écrit dans
le message de I couldn't find anything in my searches... I'm wondering if there's a Regex
(with or without additional C# code) that can convert a either
"lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with
spaces).

Thanx!
 
I should note -- this function assumes that one will only ever pass in a single lower/UpperCamelCase word (i.e. no spaces, etc).

--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com



"Greg Collins [InfoPath MVP]" <Greg.Collins_AT_InfoPathDev.com> wrote in message Thanks. I knew that had to be something fairly simple.

So which is faster -- to use the Match Evaluator, or to just blindly convert the first character ToUpper() before running the more simple Regex?

public string CamelToTitleCase(string Text)
{
Text = Text.Substring(0, 1).ToUpper() + Text.Substring(1);
return Regex.Replace(Text, @"(\B[A-Z])", @" $1");
}


--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com



To convert from UpperCamelCase to Title Case, use this line :
Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");

To convert from both lowerCamelCase and UpperCamelCase to Title Case, use
MatchEvaluator :
public string toTitleCase(Match m) {
char c=m.Captures[0].Value[0];
return ((c>='a')&&(c<='z'))?Char.ToUpper(c).ToString():" "+c;
}
and change a little your regex with this line :
Regex.Replace("UpperCamelCase or lowerCamelCase",@"(\b[a-z]|\B[A-Z])",new
MatchEvaluator(toTitleCase));

Hope it helps,

Ludovic SOEUR.


"Greg Collins [InfoPath MVP]" <Greg.Collins_AT_InfoPathDev.com> a écrit dans
le message de I couldn't find anything in my searches... I'm wondering if there's a Regex
(with or without additional C# code) that can convert a either
"lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with
spaces).

Thanx!
 
To convert from UpperCamelCase to Title Case, use this line :
Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");

To convert from both lowerCamelCase and UpperCamelCase to Title Case, use
MatchEvaluator :
public string toTitleCase(Match m) {
char c=m.Captures[0].Value[0];
return ((c>='a')&&(c<='z'))?Char.ToUpper(c).ToString():" "+c;
}
and change a little your regex with this line :
Regex.Replace("UpperCamelCase or lowerCamelCase",@"(\b[a-z]|\B[A-Z])",new
MatchEvaluator(toTitleCase));

Hope it helps,

Ludovic SOEUR.

"Greg Collins [InfoPath MVP]" <Greg.Collins_AT_InfoPathDev.com> a écrit dans
le message de I couldn't find anything in my searches... I'm wondering if there's a Regex
(with or without additional C# code) that can convert a either
"lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with
spaces).

Thanx!
 
Back
Top