EasyCFM.COM ColdFusion Forums / Good Coding Tips! / Title Case Function

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: Title Case Function -- page: 1 2

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

louissto56
11-29-2007 @ 10:37 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1149
Joined: Jan 2007

Hey guys. I just made a title case function that is quite handy so I thought I would share it with y'all. Here it is. If there are better coding methods just reply.


     <cffunction name="titleCase" access="public" returntype="string">
          <cfargument name="str" type="string" required="yes">
          <cfset casecount = 1>
        <cfloop list="#arguments.str#" delimiters=" " index="i">
             <cfif refind("^[a-z]", Left(i,1))>    
                    <cfif casecount EQ 1>
                    <cfset arguments.str = UCase(Left(i,1)) & Right(i,Len(i)-1) & " ">
                <cfelse>
                    <cfset arguments.str = arguments.str & UCase(Left(i,1)) & Right(i,Len(i)-1) & " ">
                </cfif>
                      <cfset casecount = casecount + 1>
            </cfif>
        </cfloop>
          <cfreturn arguments.str>
     </cffunction>


Notice-Board in NZ has now been released. Check it out at www.notice-board.co.nz !!

This message was edited by louissto56 on 11-29-07 @ 10:37 PM

gmilby
11-30-2007 @ 5:17 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 281
Joined: Nov 2007

how are you using this?

the code looks air tight, but curious about the application of it Smile

Never pick a fight with an ugly person, they've got nothing to lose.
-Robin Williams

CJ
11-30-2007 @ 11:49 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4262
Joined: Oct 2002

<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>


avoids the conditional.  avoids the string concatenation (altho under the hood listAppend() might be doing a simple string concatenation as well).  

you should also look at www.cflib.org.  there are several of these propercase functions up there.  always interesting to see how other people write it.

-CJ-
@ #coldfusion/DALNet
http://charlie.griefer.com

Teachers open the door. You enter by yourself.
—Chinese Proverb

louissto56
12-02-2007 @ 4:49 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1149
Joined: Jan 2007

Well gmilby, you use it to convert any case to title case. This Is Title Case.

Thanks CJ, good idea of using LCase for the rest.

Notice-Board in NZ has now been released. Check it out at www.notice-board.co.nz !!

gmilby
12-02-2007 @ 10:07 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 281
Joined: Nov 2007

the problem was i didn't know the term title case.. i use case statements (cfswitch/expression and fail safe to default case, but have never used or knew of title case by that name)
i was looking at this http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt2110.htm
in jbase/pick - we just called it 'casing'... we up cased everything for searches etc - just for consistency.
when i get time, i'll try out your function - it would be nice in some respects to use some of my old coding standards with the new web technologies i'm learning (as in standardizing all form inputs/etc so that they're formatted before queries and all work consistantly regardless of how input is entered).
thanks for sharing this - there's always room to grow and learn.

Never pick a fight with an ugly person, they've got nothing to lose.
-Robin Williams

louissto56
12-05-2007 @ 9:41 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1149
Joined: Jan 2007

You made a little error there CJ. You forgot to define the delimeter as being a space.

<cfset retStr = listAppend(retStr, ucase(left(idx,1)) & lcase(right(idx,Len(idx)-1))," ") />

Notice-Board in NZ has now been released. Check it out at www.notice-board.co.nz !!

CJ
12-06-2007 @ 3:18 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4262
Joined: Oct 2002

d'oh!

good catch Smile  

-CJ-
@ #coldfusion/DALNet
http://charlie.griefer.com

Teachers open the door. You enter by yourself.
—Chinese Proverb

louissto56
12-06-2007 @ 7:19 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1149
Joined: Jan 2007

Ahah never fear. It happens to the best of us Wink

Notice-Board in NZ has now been released. Check it out at www.notice-board.co.nz !!

jeirel
11-09-2009 @ 6:03 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 1
Joined: Jun 2009

Is there a way to use this in a SQL statement? I am trying to find a solution to convert form fields to title case before adding to a database. I've put the function you wrote in a component call titlecase.cfc and have called it. I need some help to figure out how to use it. Thanks in advance for the assistance.

gmilby
11-09-2009 @ 7:21 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 281
Joined: Nov 2007

hi, why can't you just set them after the post/before
the query?
<cfset variable = lcase(variable)>

<cfquery ....
INSERT .... variable that's been lower cased

i haven't given cf advice for a while - hope it's
pertinent... i have my head in php/python for a long
time now.



"Coder Dog" pictured above
Success is the ablity to go from one failure to another with no loss of enthusiasm.
-Sir Winston Churchill

PAGE: 1 2

Website Designed and Developed by Pablo Varando.