Here’s a simple coldfusion function to make a string title case(every word of the string starting with capital letters). You can pass in exceptions in a list.
<cffunction name=”makeTitleCase” access=”public” returntype=”string” output=”no”>
<cfargument name=”str” type=”string” required=”yes”/>
<cfargument name=”excludeList” type=”string” required=”no” default=”an,the,at,by,for,of,in,up,on,to,and,as,but,if,or,nor,a,is”/>
<cfset var i = 1>
<cfset var wordArray = “”>
<cfscript>
if(arguments.str.trim().length() EQ 0) {
return arguments.str;
}
wordArray = listToArray(arguments.str,” “);
for(i=1;i<=arrayLen(wordArray);i++) {
if(not listfindnocase(arguments.excludeList,wordArray[i])) {
wordArray[i] = wordArray[i].substring(0, 1).toUpperCase() & wordArray[i].substring(1);
}
}
</cfscript>
<cfreturn arrayToList(wordArray,” “)/>
</cffunction>
it uses some underlying JAVA functionality to manipulate the string.
Enjoy!
Advertisement