CFLib.org – Common Function Library Project

ReplaceAtNoCase(theString, oldSubString, newSubString, startIndex [, theScope])

Last updated June 26, 2002

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
An enhanced version of CF's ReplaceNoCase() function. Returns theString with occurrence(s) of oldSubString replaced with newSubString (ignoring case) in a specified scope starting from the startIndex. This startIndex feature can be especially useful for partial and/or conditional replacements.

Return Values:
Returns a string.

Example:

view plain print about
An example of a conditional replacement:<br>
<br>
<cfscript>
before_str    = "X. red&nbsp;&nbsp;X. orange&nbsp;&nbsp;X. yellow";
after_str     = before_str;
i             = 1;
replace_str   = 1;
do {
    previous_str  = after_str;
    after_str         = ReplaceAtNoCase(after_str, "x", replace_str, i);
    i             = i + Len(replace_str);
    replace_str   = replace_str +1;
} while (after_str NEQ previous_str);
</cfscript>

<cfoutput>
before:<br>
#before_str#<br>
<br>
after:<br>
#after_str#<br>
</cfoutput>
<br>
<br>
<br>


An example of a partial replacement:<br>
<br>
<cfset b_str = "xxxxxxxxxxxxxxxxxx">
<cfoutput>
before:<br>
#b_str#<br>
<br>
after:<br>
#ReplaceAtNoCase(b_str, "X", "Y", Int(Len(b_str)/2), "ALL")#<br>
</cfoutput>

Parameters:

Name Description Required
theString The string to modify. Yes
oldSubString The substring to replace. Yes
newSubString The substring to use as a replacement. Yes
startIndex Where to start replacing in the string. Yes
theScope Number of replacements to make. Default is "ONE". Value can be "ONE" or "ALL." No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Replaces oldSubString with newSubString from a specified starting position while ignoring case.
 * 
 * @param theString      The string to modify. (Required)
 * @param oldSubString       The substring to replace. (Required)
 * @param newSubString      The substring to use as a replacement. (Required)
 * @param startIndex      Where to start replacing in the string. (Required)
 * @param theScope       Number of replacements to make. Default is "ONE". Value can be "ONE" or "ALL." (Optional)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, June 26, 2002 
 */

function ReplaceAtNoCase(theString, oldSubString, newSubString, startIndex){
    var targetString  = "";
    var preString     = "";

    var theScope      = "ONE";
    if(ArrayLen(Arguments) GTE 5) theScope    = Arguments[5];

    if (startIndex LTE Len(theString)) {
        targetString = Right(theString, Len(theString)-startIndex+1);
        if (startIndex GT 1) preString = Left(theString, startIndex-1);
        return preString & ReplaceNoCase(targetString, oldSubString, newSubString, theScope);
    } else {
        return theString;
    }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson