473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex to convert "camelCase" into "Title Case"

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!

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

Nov 17 '05 #1
5 29001
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 news:%2***************@TK2MSFTNGP12.phx.gbl...
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!

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


Nov 17 '05 #2
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 news:%2***************@TK2MSFTNGP12.phx.gbl...
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!

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


Nov 17 '05 #3
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

"Ludovic SOEUR" <Lu***********@hotmail.com> wrote in message news:uv**************@TK2MSFTNGP14.phx.gbl...
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 news:%2***************@TK2MSFTNGP12.phx.gbl...
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!

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


Nov 17 '05 #4
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 news:#7**************@TK2MSFTNGP15.phx.gbl...
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

"Ludovic SOEUR" <Lu***********@hotmail.com> wrote in message news:uv**************@TK2MSFTNGP14.phx.gbl...
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 news:%2***************@TK2MSFTNGP12.phx.gbl...
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!

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


Nov 17 '05 #5
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 news:%2***************@TK2MSFTNGP12.phx.gbl...
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!

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


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Paulo da Silva | last post by:
Hi! If I have two files .py such as m.py from c import * ... x=c() ... os.any_method ...
9
by: Robbie Hatley | last post by:
Greetings, group. I just found a weird problem in a program where a variable declared in a {block} after a "case" keyword was being treated as having value 0 even though its actual value should...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.