CFLib.org – Common Function Library Project

delimitInterCap(str, delimiter, capFirst)

Last updated July 09, 2008

author

Rachel Maxim

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Adds passed delimiter (any character) before capital letters in passed InterCap/Camel Case string. Optionally capitalize the first letter in the string. Use for creating human readable file names and labels from interCap names and labels.

Return Values:
Returns a string.

Example:

<!--- add spaces and cap first letter --->
<cfset delimitIntercapString = delimitInterCap('myTestInterCapString',' ',true)>
<cfdump var="#delimitIntercapString#">

<!--- add underscores and don't cap first letter --->
<cfset delimitIntercapString = delimitInterCap('myTestInterCapString','_',false)>
<cfdump var="#delimitIntercapString#">

Parameters:

Name Description Required
str String to format. Yes
delimiter Delimiter used to format string. Yes
capFirst Boolean used to determine if first character should be set to uppercase. Yes

Full UDF Source:

/**
 * Adds delimiting character(s) before capital letters in interCap strings.
 * 
 * @param str      String to format. (Required)
 * @param delimiter      Delimiter used to format string. (Required)
 * @param capFirst      Boolean used to determine if first character should be set to uppercase. (Required)
 * @return Returns a string. 
 * @author Rachel Maxim (rmaxim@gmail.com) 
 * @version 1, July 9, 2008 
 */
function delimitInterCap(str,delimiter,capFirst) {
    var newStr = '';
    var currentChar = '';
    var replaceStr = '';
    var i = 0;
    //should the first letter be upper case?
    if (isBoolean(capFirst) and capFirst is true) {
        newStr = uCase(left(str,1));
    } else {
        newStr = left(str,1);
    }
    //loop over each character in the string starting with the second
    for (i = 2; i lte len(str); i = i + 1) {
        //get the character at this index
        currentChar = mid(str,i,1);
        //search for capital letters
        if  (reFind('[A-Z]',currentChar)) {
            //if capital, prepend with delimiter
            replaceStr = delimiter & currentChar;
            //append to the new string
            newStr = newStr & replaceStr;
        } else {
            //append original character to the new string
            newStr = newStr & currentChar;
        }
    }
    return 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