I am pretty new to CF and I am having difficulty getting a function to work the way I need it to. It's working to convert to title case unless a single lowercase string is used. I think I need to add something to have it check string length and if it is only on character, convert automatically. Not sure how to accomplish this. Any help would be greatly appreciated.
Here is the code:
<cffunction name="titleCase" access="public" returntype="string">
<cfargument name="str" type="string" required="yes" />
<cfset var retStr = "" />
<cfset var idx = "" />
<cfloop list="#arguments.str#" delimiters=" " index="idx">
<cfset retStr = listAppend(retStr, ucase(left(idx,1)) & lcase(right(idx,Len(idx)-1))," ") />
</cfloop>
<cfreturn retStr />
</cffunction>
Jeffrey,
When you say 'title case', do you mean capitalizing the first letter of each word? I might not be understanding your request completely. However, if I did understand it correctly, check out CFLib.org:
http://cflib.org/udf/CapFirstTitle
The cap first title function would seem to fit your needs. You can also search CFLib for loads of other helpful functions for such tasks.
Best,
Craig
Hi Plz try this,
I understood u have an iput like "This is a pen". and output wil be "This Is A Pen" rt?
In your code error occur a single letter came. i solved this issue. Plz try this and let me know if it is helpful.
<cffunction name="titleCase" access="public" returntype="string">
<cfargument name="str" type="string" required="yes" />
<cfset var retStr = "" />
<cfset var idx = "" />
<cfloop list="#arguments.str#" delimiters=" " index="idx">
<cfif Len(idx) gt 1>
<cfset lcasert=lcase(right(idx,Len(idx)-1))>
<cfset retStr = listAppend(retStr, ucase(left(idx,1)) & lcasert," ")>
<cfelse>
<cfset setnull=" ">
<cfset retStr = listAppend(retStr, ucase(left(idx,1)) & setnull," ")>
</cfif>
</cfloop>
<cfreturn retStr />
</cffunction>
Use the function at http://www.cflib.org/udf/CapFirstTitle.
North America
Europe, Middle East and Africa
Asia Pacific