Skip navigation
JeffIreland
Currently Being Moderated

Change Case

Jun 24, 2009 3:49 PM

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>

 
Replies
  • Currently Being Moderated
    Jun 24, 2009 4:13 PM   in reply to JeffIreland

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2009 7:12 PM   in reply to JeffIreland

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2009 10:47 PM   in reply to Dan Bracuk

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2009 11:58 PM   in reply to cfchitchat

    Is my post helpful?plz respond plzzz

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 30, 2009 8:34 AM   in reply to JeffIreland
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points