CFLib.org – Common Function Library Project

StripBlock(strString, strFirstChar, strLastChar[, strScope])

Last updated June 21, 2002

author

Neal Barnett

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Removes all characters in a string between two characters. Especially useful for remarks enclosed in brackets, asterisks, etc., which you don't wish to display.

Return Values:
Returns a string.

Example:

<cfset strvar = StripBlock("Show them this [but not this]","[","]")>
<cfoutput>#strvar#</cfoutput>

Parameters:

Name Description Required
strString String to modify. Yes
strFirstChar Character to begin removal. Yes
strLastChar Character to end removal. Yes
strScope ALL or ONE. Signifies how many replacements to make. Default is ALL. No

Full UDF Source:

/**
 * Removes all characters in a string between two characters.
 * 
 * @param strString 	 String to modify. (Required)
 * @param strFirstChar 	 Character to begin removal. (Required)
 * @param strLastChar 	 Character to end removal. (Required)
 * @param strScope 	 ALL or ONE. Signifies how many replacements to make. Default is ALL. (Optional)
 * @return Returns a string. 
 * @author Neal Barnett (nealb@800wine.com) 
 * @version 1, June 21, 2002 
 */
function StripBlock(strString,strFirstChar,strLastChar) 
{
	// Special Chars - Don't include ] (end-bracket) since it must be the
	// first character within brackets [ ] in the regular expression
	var strSpecialChars = "+*?.[^$(){}|\&##-";
	
	// Default to replace all blocks in string unless they passed a scope
	var strScope = "ALL";  
	if (ArrayLen(Arguments) gt 3)
	  	strScope = Arguments[4];
		
	// Escape the start and end chars if they're special
	if (FindNoCase(strFirstChar,strSpecialChars)) 
		strFirstChar = "\" & strFirstChar;
	if (FindNoCase(strLastChar,strSpecialChars)) 
		strLastChar = "\" & strLastChar;

	return REReplaceNoCase(strString,strFirstChar & "[^" & strLastChar & "]*" & strLastChar,"","#strScope#");
}
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