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") />

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


  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.

  5. Shane says:

    Thanks for the CSS version of this! Exactly what I was looking for. 🙂

  6. Matt Wadsworth says:

    Hi Ryan,

    Thanks a lot for pointing me in the direction of the We Code Things blog.
    That is going to come in very handy indeed.



  7. Brandon says:

    I just started learning CF and this is exactly what I was looking for. Thanks to Jake for his suggestion too.

    If anyone is looking to do this in php it's simply ucwords($variable or text);


  8. Bobby Tan says:

    @Jake: That's the first thing I thought of too. Great minds and all that. 🙂

    @Brandon: Thanks for the PHP version.

    The WeCodeThings blog is an excellent resource. Thanks!