ColdFusion - InitCap

Article Information

Article Content

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"

Our Mission

At Wildlink, our mission is to deliver the best business solutions we can to enable our clients to gather and unlock the answers in their data.
We are committed to fostering long-term partnerships built on trust, integrity, and a relentless pursuit of innovation.