function to make string title case

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.