Converting a string to title case in ColdFusion

Thursday 22 December 2011

The following function takes a string and converts it to title case.

<cffunction name="titleCase" returntype="string" output="FALSE">
  <cfargument name="str" type="string" required="TRUE" />

  <cfset var newstr = "" />
  <cfset var word = "" />

  <cfloop list="#arguments.str#" index="word" delimiters=" ">
    <cfset newstr &= " " & Ucase( Left( word, 1 ) ) />
    
	<cfif Len( word ) > 1>
      <cfset newstr &= Lcase( Right( word, Len( word ) - 1 ) ) />
    </cfif>
  </cfloop>

  <cfreturn Trim( newstr ) />
</cffunction>

For example, the string "COLDFUSION ROCKS!!!" will be converted to "Coldfusion Rocks!!!".

<cfdump var="#titleCase( "COLDFUSION ROCKS!!!" )#">

Update 24-Dec-11: See Peter's solution below - much less code than my approach! :)


Tags

ColdFusion (13)

Share


Comments

Carl Von Stetten Might want to add a Trim function to your return, because the string will have a space in the front.

Posted by Carl Von Stetten on Thursday 22 December 2011 at 16:59 GMT

Simon Bingham Good point. Have amended. Thanks Carl. :)

Posted by Simon Bingham on Thursday 22 December 2011 at 17:02 GMT

Carl Von Stetten My pleasure, sir. :-)

Posted by Carl Von Stetten on Thursday 22 December 2011 at 17:07 GMT

John Whish Don't know if you know, but in CF8+ you can save yourself some typing so instead of:

<cfset foo = foo & " " & bar>
you can do:
<cfset foo &= " " & bar>

String concatination in ColdFusion is kinda slow, so this could be an issue if you were converting a large block of text to title case. There are a couple of ways to improve the performance if you want to, I did a post on it a while back:
http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/string-concatenation-performance-test-128

Or for pretty graphs have a look at the post by Jason Delmore:
http://www.cfinsider.com/index.cfm/2009/10/23/High-Performance-String-Concatenation-in-ColdFusion

Posted by John Whish on Thursday 22 December 2011 at 17:41 GMT

Simon Bingham Thanks John. You did tell me, but I promptly forgot. I've amended the function. :)

Posted by Simon Bingham on Friday 23 December 2011 at 10:16 GMT

Peter Boughton That's an awful lot of effort for something so simple...

<cfreturn rereplace( Arguments.Str , '\b(\S)(\S*)\b' , '\u\1\L\2' , 'all' ) />

:)

Posted by Peter Boughton on Friday 23 December 2011 at 16:16 GMT

Simon Bingham Wow! That's very cool. Thanks Peter. I think learning regular expressions needs to go on my list of new year's resolutions! :)

Posted by Simon Bingham on Saturday 24 December 2011 at 15:07 GMT

Adam Cameron This is an interesting exercise, and I have to say that Peter's solution is a pleasingly terse one. It doesn't work for my father's surname "O'Brien" though. Still: that's an edge case. It worked for everything else my 2min of testing threw at it (although that was the very first thing I threw at it).

However what has made me decide that automating this sort of thing is a fool's errand is that it's very seldom the case that one WANTS what those functions spit out. When capitalising a sentence, it looks jarring if every single word (including articles, prepositions, conjunctions etc) is capitalised.

My approach is that it's a human task to get the text right, and according to the editorial policy of the website (because there is no standard practice for doing this either, so it requires a house rule). If the text is entered by human copywriters, then they need to know the house rules, and there should be oversight that they're getting it right. If it's stuff entered by the hoi polloi, then it needs checking before it goes live, but only if you give a sh!t that gen-pop-entered text is formatted all nice. I think it would be seldom that you'd care.

This is a good summary as to why there's more to this than just a regex susbstitution:
http://en.wikipedia.org/wiki/Title_case#Headings_and_publication_titles

What I found interesting is (and I didn't know or notice this), that the Guardian (whose policy, if not - notoriously - their execution of their own policy, is something to be admired: http://www.guardian.co.uk/styleguide) that they don't use cap-first at all. They use sentence-case. And it looks tidy. I claim the "clumsiest sentence of the day" award for the first one of this para, btw.

Food for thought, perhaps, on a day after many people have had way too much of the more tangible type of food ;-)

--
Adam

Posted by Adam Cameron on Monday 26 December 2011 at 14:29 GMT

Simon Bingham Thanks for your comments Adam. I think you raise some excellent points. I've actually converted the titles of my posts on this blog to sentence case and I agree it makes them much more readable.

I also agree that both Peter's method and my original method aren't ideal. I've noticed that my example has an error because the 'F' in 'ColdFusion' should be, of course, uppercase! :)

I originally required a titleCase method because I had a large spreadsheet of products to import into a database and the product titles were a mixture of upper and lower case. I think that, in this scenario, at least the converted titles will be consist.

Posted by Simon Bingham on Wednesday 28 December 2011 at 10:35 GMT

Add Comment

Your email address will not be displayed on this blog.

The URL of your blog or web site.