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

ColdFusion, Tips and Tricks, Technology, CFML, UDF
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>
Jean Moniatte said:
 
Looks like you need to add a lCase to make it work: REReplaceNoCase(lCase(arguments.string),"(^[a-z*]|[ *][a-z*])","\U\1\E","all").
 
posted 193 days ago
Add Comment Reply to: this comment OR this thread
 
 
Have you tried it? Isn't that kind of the point of the "NoCase" in REReplaceNoCase?
 
posted 193 days ago
Add Comment Reply to: this comment OR this thread
 
 
Oh hell... My bad Jean. You are right and I just corrected it. I had that in the one in my library, but when I was typing it in I thought "I don't need that LCase!". I had forgotten why I had put it in there in the first place!
 
posted 193 days ago
Add Comment Reply to: this comment OR this thread
 
Jean Moniatte said:
 
Yes, I tried (thanks for posting by the way, it was perfect timing). The NoCase in REReplaceNoCase just tells to not worry about case when matching the regExp. Without the lCase, "DAVID B. SHUCK" stays the same...
 
posted 193 days ago
Add Comment Reply to: this comment OR this thread
 
Jean Moniatte said:
 
Hey, no problem at all! Thanks again for posting in the first place, it is very handy.
 
posted 193 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Various Links

HikeTheCanyon.org - Rim-to-Rim hike of the Grand Canyon.
Dave Shuck on Twitter - Follow me on Twitter.
Scriptalizer - Minify your Javascript and CSS