Reputation: 14208
Using ColdFusion, I'd like to convert camelCase strings into a human readable string, like:
firstName -> First Name
Also, this will ideally be done all inline, with something like Ucase(rereplace('myCamelCaseString',[regex]," "))
. If inline is not possible, then a UDF perhaps?
Upvotes: 2
Views: 5226
Reputation: 1
Custom camalCase method
<cfset variables.string = 'I am developer'>
<cfset variables.counter = 1>
<cfset variables.actualString = ''>
<cfloop list="#variables.string#" index="i" delimiters=" ">
<cfif variables.counter EQ 1>
<cfset variables.actualString = ReplaceNoCase(i, Left(variables.string, 1), Left(LCase(variables.string), 1), "ONE")>
<cfelse>
<cfset variables.actualString = variables.actualString &''& ucFirst(i)>
</cfif>
<cfset variables.counter = variables.counter + 1>
</cfloop>
<cfdump var="#variables.actualString#">
<cfabort>
Upvotes: 0
Reputation: 6430
CFLib is your friend!
There's camelToSpace() which does what you're asking, except for capitalizing.
<cfscript>
/**
* Breaks a camelCased string into separate words
* 8-mar-2010 added option to capitalize parsed words Brian Meloche [email protected]
*
* @param str String to use (Required)
* @param capitalize Boolean to return capitalized words (Optional)
* @return Returns a string
* @author Richard ([email protected]@trilobiet.nl)
* @version 0, March 8, 2010
*/
function camelToSpace(str) {
var rtnStr=lcase(reReplace(arguments.str,"([A-Z])([a-z])"," \1\2","ALL"));
if (arrayLen(arguments) GT 1 AND arguments[2] EQ true) {
rtnStr=reReplace(arguments.str,"([a-z])([A-Z])","\1 \2","ALL");
rtnStr=uCase(left(rtnStr,1)) & right(rtnStr,len(rtnStr)-1);
}
return trim(rtnStr);
}
</cfscript>
If you want to capitalize each word in the resulting string, there's CapFirstTitle()
<cfscript>
/**
* Returns a string with words capitalized for a title.
* Modified by Ray Camden to include var statements.
* Modified by James Moberg to use structs, added more words, and reset-to-all-caps list.
*
* @param initText String to be modified. (Required)
* @return Returns a string.
* @author Ed Hodder ([email protected])
* @version 3, October 7, 2011
*/
function capFirstTitle(initText){
var j = 1; var m = 1;
var doCap = true;
var tempVar = "";
/* Make each word in text an array variable */
var Words = ListToArray(LCase(trim(initText)), " ");
var excludeWords = structNew();
var ResetToALLCAPS = structNew();
/* Words to never capitalize */
tempVar = ListToArray("a,above,after,ain't,among,an,and,as,at,below,but,by,can't,don't,for,from,from,if,in,into,it's,nor,of,off,on,on,onto,or,over,since,the,to,under,until,up,with,won't");
for(j=1; j LTE (ArrayLen(tempVar)); j = j+1){
excludeWords[tempVar[j]] = 0;
}
/* Words to always capitalize */
tempVar = ListToArray("II,III,IV,V,VI,VII,VIII,IX,X,XI,XII,XIII,XIV,XV,XVI,XVII,XVIII,XIX,XX,XXI");
for(j=1; j LTE (ArrayLen(tempVar)); j = j+1){
ResetToALLCAPS[tempVar[j]] = 0;
}
/* Check words against exclude list */
for(j=1; j LTE (ArrayLen(Words)); j = j+1){
doCap = true;
/* Word must be less than four characters to be in the list of excluded words */
if(LEN(Words[j]) LT 4){
if(structKeyExists(excludeWords,Words[j])){ doCap = false; }
}
/* Capitalize hyphenated words */
if(ListLen(trim(Words[j]),"-") GT 1){
for(m=2; m LTE ListLen(Words[j], "-"); m=m+1){
tempVar = ListGetAt(Words[j], m, "-");
tempVar = UCase(Mid(tempVar,1, 1)) & Mid(tempVar,2, LEN(tempVar)-1);
Words[j] = ListSetAt(Words[j], m, tempVar, "-");
}
}
/* Automatically capitalize first and last words */
if(j eq 1 or j eq ArrayLen(Words)){ doCap = true; }
/* Capitalize qualifying words */
if(doCap){ Words[j] = UCase(Mid(Words[j],1, 1)) & Mid(Words[j],2, LEN(Words[j])-1); }
if (structKeyExists(ResetToALLCAPS, Words[j])) Words[j] = ucase(Words[j]);
}
return ArrayToList(Words, " ");
}
</cfscript>
So, once you have those UDFs in place, you can do
CapFirstTitle(camelToSpace('myCamelCaseString'))
which will return My Camel Case String
.
Upvotes: 4
Reputation: 20070
Here are two functions that work together to convert a string of words into camel case. Words can be separated by a space or underscore, although you can add other characters as necessary.
<cffunction name="camelCase" access="public" output="false" returntype="string">
<cfargument name="sourceString" type="string" required="true">
<cfscript>
var s = LCase(Trim(arguments.sourceString));
s = camelCaseByWordSeperator(s, " ");
s = camelCaseByWordSeperator(s, "_");
return s;
</cfscript>
</cffunction>
<cffunction name="camelCaseByWordSeperator" access="private" output="false" returntype="string">
<cfargument name="sourceString" type="string" required="true">
<cfargument name="separator" type="string" required="false" default="_">
<cfscript>
var s = arguments.sourceString;
var wordBreakPos = Find(arguments.separator, s);
while (wordBreakPos gt 0) {
lens = Len(s);
if (wordBreakPos lt lens) {
s = Replace(Left(s, wordBreakPos), arguments.separator, "", "all") & UCase(Mid(s, wordBreakPos+1, 1)) & Right(s, lens - wordBreakPos - 1);
} else {
s = Replace(s, arguments.separator, "", "all");
}
wordBreakPos = Find(arguments.separator, s);
}
return s;
</cfscript>
</cffunction>
Upvotes: 0
Reputation: 3556
#rereplace("camelCaseString","([A-Z])"," \1","all")#
edit: the version below will handle the lowercase first character.
#rereplace(rereplace("camelCaseString","(^[a-z])","\u\1"),"([A-Z])"," \1","all")#
Upvotes: 12
Reputation: 1167
I don't think you can do it in one go with a regex, because they don't support recursion/iteration, so you can't make it work on strings with any number of wordsPushedTogether.
You could just do a loop where you start with a blank string, loop over the camelCase string, and every time you find a capital letter, split off the letters before it and append it to your new string with a space.
Upvotes: -3