Skip to content
February 25, 2010 / dshuck

UDF: pcase() – Captilize first letter of each word in a string

This is a UDF that I keep on hand for converting strings to “proper case”, such as a person’s name.  For instance, in a database we might have a name stored like DAVID B. SHUCK, but when they log into our site and we welcome them, we don’t necessarily want to shout it at them!   Saying “Hello David B. Shuck…” would hopefully be a bit less abrasive.

Enter the user-defined function pcase().  Through the use of regular expressions we are doing a search for word patterns and capitalizing the first letter in each word.  Enjoy!


<cffunction name="pcase" access="public" output="false"  returntype="string">
    <cfargument name="string"  type="string" required="true" />
    <cfreturn  REReplaceNoCase(LCase(arguments.string),"(^[a-z*]|[ *][a-z*])","\U\1\E","all")  />
</cffunction>

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.

Join 591 other followers