Skip to main content
Participant
June 24, 2009
Question

Change Case

  • June 24, 2009
  • 3 replies
  • 969 views

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>

    This topic has been closed for replies.

    3 replies

    davidsimms
    Inspiring
    June 30, 2009

    Use the function at http://www.cflib.org/udf/CapFirstTitle.

    Inspiring
    June 25, 2009

    Your existing code appears to make each word capitalized.

    This sentence - "It's working to convert to title case unless a single lowercase string is used." is only clear until the word unless.

    June 25, 2009

    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>

    June 27, 2009

    Is my post helpful?plz respond plzzz

    Inspiring
    June 24, 2009

    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