CFLib.org – Common Function Library Project

delimitInterCap(str, delimiter, capFirst)

Last updated July 9, 2008
Download UDF

author

Rachel Maxim Rachel Maxim

Version: 1 | Requires: ColdFusion 5 | 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:

<cfscript>
/**
* 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;
}
</cfscript>

Search CFLib.org


Latest Additions

Topper Topper added
getCurrentURL
1 hour(s) ago

Daniel Chicayban Daniel Chicayban added
IsListInList
1 day(s) ago

Jeff Howden Jeff Howden added
CSVFormat
9 day(s) ago

Tom de Manincor Tom de Manincor added
flattenStruct
10 day(s) ago

Troy Pullis Troy Pullis added
iCalUS
10 day(s) ago

Created by Raymond Camden / Design by Justin Johnson