CFLib.org – Common Function Library Project

StripAllBut(strSource, strKeep[, beCS])

Last updated July 2, 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");
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson