CFLib.org – Common Function Library Project

CapFirstTitle(initText)

Last updated July 27, 2001
Download UDF

author

Ed Hodder                                         Ed Hodder

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 8 time(s). Average Rating: 3.9

Description:
Takes a string and returns it capitalized for a title. Always capitalizes first and last words and hyphenated words. Leaves some prepositions and conjunctions lower case.

Return Values:
Returns a string.

Example:

<CFSET str="once upon a time in the west">
<CFOUTPUT>
Given str=#str#<BR>
The CapFirstTitle version is #CapFirstTitle(str)#
</CFOUTPUT>

Parameters:

Name Description Required
initText String to be modified. Yes

Full UDF Source:

<cfscript>
/**
* Returns a string with words capitalized for a title.
* Modified by Ray Camden to include var statements.
*
* @param initText      String to be modified.
* @return Returns a string.
* @author Ed Hodder (ed.hodder@bowne.com)
* @version 2, July 27, 2001
*/

function capFirstTitle(initText){
    
    var Words = "";
    var j = 1; var m = 1;
    var doCap = "";
    var thisWord = "";
    var excludeWords = ArrayNew(1);
    var outputString = "";
    
    initText = LCASE(initText);
    
    //Words to never capitalize
    excludeWords[1] = "an";
    excludeWords[2] = "the";
    excludeWords[3] = "at";
    excludeWords[4] = "by";
    excludeWords[5] = "for";
    excludeWords[6] = "of";
    excludeWords[7] = "in";
    excludeWords[8] = "up";
    excludeWords[9] = "on";
    excludeWords[10] = "to";
    excludeWords[11] = "and";
    excludeWords[12] = "as";
    excludeWords[13] = "but";
    excludeWords[14] = "if";
    excludeWords[15] = "or";
    excludeWords[16] = "nor";
    excludeWords[17] = "a";
    
    //Make each word in text an array variable
        
    Words = ListToArray(initText, " ");
    
    //Check words against exclude list
    for(j=1; j LTE (ArrayLen(Words)); j = j+1){
        doCap = true;
        
        //Word must be less that four characters to be in the list of excluded words
        if(LEN(Words[j]) LT 4 ){
            if(ListFind(ArrayToList(excludeWords,","),Words[j])){
                doCap = false;
            }
        }

        //Capitalize hyphenated words        
        if(ListLen(Words[j],"-") GT 1){
            for(m=2; m LTE ListLen(Words[j], "-"); m=m+1){
                thisWord = ListGetAt(Words[j], m, "-");
                thisWord = UCase(Mid(thisWord,1, 1)) & Mid(thisWord,2, LEN(thisWord)-1);
                Words[j] = ListSetAt(Words[j], m, thisWord, "-");
            }
        }
        
        //Automatically capitalize first and last words
        if(j eq 1 or j eq ArrayLen(Words)){
            doCap = true;
        }
        
        //Capitalize qualifying words
        if(doCap){
            Words[j] = UCase(Mid(Words[j],1, 1)) & Mid(Words[j],2, LEN(Words[j])-1);
        }
    }
    
    outputString = ArrayToList(Words, " ");

    return outputString;

}
</cfscript>

Search CFLib.org


Latest Additions

Ryan Thompson-Jewell Ryan Thompson-Jewell added
ListSplit
13 hour(s) ago

Nathan Dintenfass Nathan Dintenfass added
RowsToColumns
13 hour(s) ago

Barney Boisvert Barney Boisvert added
indentXml
23 hour(s) ago

Barney Boisvert Barney Boisvert added
REReplaceCallbac...
23 hour(s) ago

Top Rated

Rob Brooks-Bilson                                 FolderSize
Rated 5.0, 7 time(s)

Nick Giovanni                                     UniqueValueList
Rated 5.0, 5 time(s)

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Stephen Withington RandomizeString
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson