CFLib.org – Common Function Library Project

CapFirst(string)

Last updated April 25, 2016

author

Raymond Camden

Version: 3 | Requires: CF10 | Library: StrLib

Description:
Returns the string with the first character of each word capitalized.

Return Values:
Returns a string.

Example:

<CFSET str="the dog jumped over the fox.">
<CFOUTPUT>
Given str=#str#<BR>
The CapFirst version is #CapFirst(str)#
</CFOUTPUT>

Parameters:

Name Description Required
string String to be modified. Yes

Full UDF Source:

<!---
 Capitalizes the first letter in each word.
 Made udf use strlen, rkc 3/12/02
 v2 by Sean Corfield.
 v3 by Chris Jordan
 
 @param string      String to be modified. (Required)
 @return Returns a string. 
 @author Raymond Camden (ray@camdenfamily.com) 
 @version 3, March 9, 2007 
--->
public string function capFirst(str){
    var local = {};

    local.newstr = '';
    local.word = '';
    local.separator = '';
    local.str = lcase(arguments.str);

    for(local.i = 1; local.i <= listlen(local.str,' '); local.i++){
        local.word = listgetat(local.str,local.i,' ');
        if(refind("^mc+",local.word) || refind("^o'+",local.word)){
            local.newstr &= local.separator & ucase(left(local.word,1)) & mid(local.word,2,1) & ucase(mid(local.word,3,1));
            if(len(local.word) > 3){
                local.newstr &= right(local.word,len(local.word)-3);
            }
        }
        else{
            //just a regular word...
            local.newstr &= local.separator & ucase(left(local.word,1));
            if(len(local.word) > 1){
                local.newstr &= right(local.word,len(local.word)-1);
            }
        }

        local.separator = ' ';
    }

    return local.newstr;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson