CFLib.org – Common Function Library Project

StripAllBut(strSource, strKeep[, beCS])

Last updated July 02, 2002

author

Scott Jibben

Version: 1 | Requires: CF5 | Library: StrLib

Description:
This generic UDF can be used to strip unwanted characters from strings. It can be used to eliminate non-numeric characters from phone numbers or social security numbers. It could also be used to strip out monetary or number formatting symbols for easier conversion back to a numeric value.

Return Values:
Returns a string.

Example:

<cfset Fax = "612-555-1234 (Fax)">
<cfoutput>
  Input: #Fax#<br>
  Strip all but the numbers: #StripAllBut(Fax, "1234567890")#<br>
  Strip all but letters: #StripAllBut(Fax, "abcdefghijklmnopqrstuvwxyz",false)#<br>
  Strip all but (cs) letters: #StripAllBut(Fax, "abcdefghijklmnopqrstuvwxyz")#<br>
</cfoutput>

Parameters:

Name Description Required
strSource The string to strip. Yes
strKeep List of characters to keep. Yes
beCS Boolean that determines if the match should be case sensitive. Default is true. No

Full UDF Source:

/**
 * Strips all characters from a string except the ones that you want to keep.
 * 
 * @param strSource      The string to strip. (Required)
 * @param strKeep      List of  characters to keep. (Required)
 * @param beCS      Boolean that determines if the match should be case sensitive. Default is true. (Optional)
 * @return Returns a string. 
 * @author Scott Jibben (scott@jibben.com) 
 * @version 1, July 2, 2002 
 */
function stripAllBut(str,strip) {
    var badList = "\";
    var okList = "\\";
    var bCS = true;

    if(arrayLen(arguments) gte 3) bCS = arguments[3];

    strip = replaceList(strip,badList,okList);
    
    if(bCS) return rereplace(str,"[^#strip#]","","all");
    else return rereplaceNoCase(str,"[^#strip#]","","all");
}

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