Capitalizing the first letter of each word – a common need for sure. There is a function for this on cflib but it didn’t work for me. No errors but it wasn’t returning what I expected. I came across this function that works, and does so with much less code:

<cffunction name="CapFirst" access="public" output="false" returntype="String">
        <cfargument name="inputString" required="false" type="String" default="" />
        <cfreturn rereplace(lcase(arguments.inputString), "(\b\w)", "\u\1", "all") />
</cffunction>

I found this snippet on the WeCodeThings blog. Good stuff.

4 Comments

  1. Jake says:

    You can do this with CSS too:  element { text-transform:capitalize; }

  2. Ryan says:

    Thats a great idea, that will work in many cases but it doesn't seem to work where I needed it yesterday – inside a select option.

  3. ziggy says:

    That ruins acronyms and other all-cap words.

  4. Ryan says:

    True, perhaps it could be improved upon.  In my case the text I was working on was all caps already, so adding a check to leave all cap words would not have worked for me.

Leave a Reply