Xindi CMS

Xindi CMS is a small, lightweight and flexible content management system. It's great for web developers looking for a simple way to make site content editable by their clients.

Open source, free and flexible, Xindi is available in CFML and PHP versions.

Find out more »

About Me

I am a web developer living and working in Exeter, Devon (United Kingdom). I use my blog to document and share the things I learn and I hope fellow developers will find it to be a useful resource.

Read more »

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

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

Update 7 April 2012: Code sample revised to use solution suggested by Peter below.


Tags


Share


Comments

Thursday, December 22, 2011 Carl Von Stetten

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


Thursday, December 22, 2011 Simon Bingham

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


Thursday, December 22, 2011 Carl Von Stetten

Carl Von Stetten My pleasure, sir. :-)


Thursday, December 22, 2011 John Whish

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


Friday, December 23, 2011 Simon Bingham

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


Friday, December 23, 2011 Peter Boughton

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' ) />

:)


Saturday, December 24, 2011 Simon Bingham

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! :)


Monday, December 26, 2011 Adam Cameron

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


Wednesday, December 28, 2011 Simon Bingham

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.


Add Comment

Your email address will not be displayed on this blog.

The URL of your blog or web site.

2 + 1 =
Host Media ColdFusion Hosting
"That's what's cool about working with computers. They don't argue, they remember everything, and they don't drink all your beer."

Paul Leary