Once I wrote this couple of functions to produce titles with the first letter of each word in upper case. Later I found out that Ray Camden had published a better version of it with exclusions for words and single letters like “and”, “a”, “the”, etc… You may find his function here at CFLIB.

Anyway, I have my functions published here just in case someone whish to use them.

<cfscript>
// Capitalize first letter of all words of a phrase
// Author: Ricardo Parente
// Date: Jan 20th 2002
function capitalize(str){
newStr = '';
aList = listToArray(str,' ');
for (i=1; i lte arrayLen(aList); i = i+1) {
newStr = newStr & cap(aList[i]) & ' ';
}
return trim(newStr);
}
</cfscript>

<cfscript>
// Capitalize the first letter of a string
// Author:Ricardo Parente
// Date: Jan 20th 2002
function cap(str){
return right(insert(ucase(left(str,1)),str,1),len(str));
}
</cfscript>

3 thoughts on “Capitalize First Letters of Each Word in a Title

  1. newStr = newStr & cap(aList[i]) & ‘ ‘;

    What is cap()

    This is referencing a function that doesn’t exist… ?

  2. add this line of code just after the declaration for separator to make it case-insensitive (otherwise strings that are all caps will still be all caps)

Leave a Reply