Friday, October 8, 2010

Caps First Title -- Capitalize the first letter of each item in a list

A while ago I needed to capitalize the first letter of every item in a list.  I created a handy dandy UDF in Coldfusion.

Capitalize the first letter of each item in a list.

@param str      List to parse. (Required)
@param delimiter      List delimiter. Defaults to a comma. (Optional)
@return Returns a string.
@author Randy Johnson (
@version 1, May 30, 2010

<cffunction name="capFirstList" returntype="string" output="false">
<cfargument name="str" type="string" required="true" />
<cfargument name="delimiter" default="," required="false">

<cfset var newstr = "" />
<cfset var word = "" />
<cfset var separator = "" />

<cfloop index="word" list="#arguments.str#" delimiters="#arguments.delimiter#">
<cfset newstr = newstr & separator & UCase(left(word,1)) />
<cfif len(word) gt 1>
<cfset newstr = newstr & right(word,len(word)-1) />
<cfset separator = arguments.delimiter />

<cfreturn newstr />

So what do you think?  How can I improve on this function?