about - tech blog -
wildlink.com

Wildlink's Technology Blog
An occasionally updated list of informative articles.
Pulled from our internal wiki.
ColdFusion - InitCap
2023-10-13

Problem

You need to return a string with the first letter of every word in uppercase.

Solution

Use listMap and break out the resultant words to fix the string.

string function initCap(required string phrase) {
    // map over the string dividing words by spaces
    return listMap(
        arguments.phrase,
        function(word) {
            return '#uCase(left(word, 1))##lCase(right(word, len(word) - 1))#';
        },
        ' '
    );
}

This is included in our common_utils module available in all 5.5 or newer apps (older apps can find it in the common_utls.cfm file).

Usage

#initCap('the quick BrOwN fox')#

outputs: "The Quick Brown Fox"

Back to the Tech Blog
Blog engine: 1.4.0