delimitInterCap(str, delimiter, capFirst)
Last updated July 9, 2008
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:
<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
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)