CFLib.org – Common Function Library Project

delimitInterCap(str, delimiter, capFirst)

Last updated July 9, 2008

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<!--- 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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson