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#");
}

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