Saturday, June 11, 2005

Coldfusion - Proper Case For Names

Format a string so that it appears like a properly formatted name. For example: "daVId" becomes "David" or "dr. bones" becomes "Dr. Bones". The code uses a single line of semi-complex Regex through the REReplace() function. First it changes the input string to lower case, then it searches for all instances of: a) a character at the beginning of the string, or b) a character preceeded by a blank (tab or space) ..and replaces the match with it's uppercase equivalent..

Associated Code :
<cfparam name="Variables.fullName" default="dr. bones">

<cfset Variables.formattedName = REReplace(LCase(Variables.fullName), "(^[[:alpha:]]|[[:blank:]][[:alpha:]])", "\U\1\E", "ALL")>

<cfoutput>#Variables.formattedName#</cfoutput>

1 Comments:

Blogger JP said...

This works well for simple names, but can you suggest how to make it work for compound names (e.g., Smith-Jones) or for O' ??

7:56 PM  

Post a Comment

<< Home