Simple InitCap function for coldfusion
<cffunction name="initcap" returntype="string" output="no">
<cfargument name="str" required="yes" type="string">
<cfset var capStr = "">
<!--- Uppercase the first character --->
<cfset capStr = ucase(left(arguments.str,1)) & lcase(mid(arguments.str,2, len(arguments.str)-1))>
<cfreturn capStr>
</cffunction>
1956 viewed | 7 opinion(s) | del.icio.us | Digg it | Tjarko @ 14/12/06 12:35 cet
Sam Farmer wrote.... (
mail)
Nice.
This also works as well:
capStr = ReReplace(capStr,"(.)","\u\1")
14 December 2006 15:02 cet
Tjarko wrote....
Even better.. short coding.. fewer characters.. nice!!
14 December 2006 15:03 cet
Ben Nadel wrote.... (
site)
One more twist on the RegEx method:
REReplace( capStr, "([a-z]{1})", "\U\1", "ONE" )
By telling it explicitly it has to macth on lower case values only, then it only performs the uppercase when required. Using the (.) notation, it will always perform the uppercase, even if the first letter is already upper case.
Cheers.
14 December 2006 22:34 cet
Steven Erat wrote.... (
site)
Nice... I've been using a less than elegant home grown solution for this.
07 February 2008 17:07 cet
Steve wrote....
Sorry, but INITCAP (in Oracle) changes the first letter of a word or series of words into uppercase.Also, it notes the presence of symbols, and will INITCAP any letter that follows a space or a symbol, such as a comma, period, colon, seimcolon, !, @, #, $, and so on.The examples given here will only uppercase the 1st occurrence of a lower case letter
19 February 2008 22:24 cet
True.. but I wanted a function that would only uppercase the first letter. You can easily change this function to do what the Oracle function does and uppercase every letter of a word, but that was not my intention.
20 February 2008 9:02 cet
But, REReplace( capStr, "([a-z]{1})", "\U\1", "ONE" ) will uppercase the 1st lower case letter it encounters, not necessarily the first.So, "THis might not be what you had in mind"would be returned "THIs might not be what you had in mind".One would assume the intent was to have a string returned with ONLY the FIRST alphabetic character uppercased.
22 February 2008 18:18 cet