PDA

View Full Version : findAndReplace string function



iloveitaly
January 20th, 2005, 06:30 PM
I have been in need of a find and replace function in flash for awhile now, so i whipped up one today. It works identical (almost) to PHP's str_replace() function.


/**
*@description: a find and replace function that will search a string and find all occurances of "search" and replace it with "replace"
*
*@param: find, a string, or an array of strings specfying what string(s) you want to find
*@param: replace, a string, or an array of string specifying what to replace the strings found with.
*if no value is specified, a empty string ("") replace value is assumed. If there are less entries in the replace array than in the find
*array, empty string values are assumes for the rest of the replace values. This value is optional.
*@param: limit, a limit to the amount of times to exacute the find & replace operation. This value is optional.
*@returns: the string after the find and replace operations have been completed
**/
String.prototype.findAndReplace = function(find, replace, limit) {
var tempString = this, tempParts, pos, loopLength;

if(typeof(find)!=="string") {//then the find and replace is an array
if(replace === undefined) replace = [""]; //if no replace was specified
else if(typeof(replace)==="string") replace = [replace]; //if only one replace was specified
} else {//then they are a string
//put both of them in array form
find = [find];
replace = [relace];
}

loopLength = find.length; //determines the loop length

for(var a = 0, c = 0; a<loopLength; a++, c = 0) {
while((pos = tempString.indexOf(find[a]))!==-1) {
if(limit!==undefined && c>=limit) break;

//split the array into 3 parts
tempParts = new Array(tempString.substring(0, pos), tempString.substr(pos, find[a].length), tempString.substr(pos+find[a].length));
tempParts[1] = (replace[a]===undefined)? ("") : replace[a]; //if there was no replace specified give an empty string
tempString = tempParts[0]+tempParts[1]+tempParts[2];

c++; //increase the counter
}
}

return tempString;
}

Leet
January 20th, 2005, 09:29 PM
Nice job, looks pretty good. One problem I did find one minor error. You had "relace" instead of "replace" in one of your lines. I corrected it for yah ;)

/**
*@description: a find and replace function that will search a string and find all occurances of "search" and replace it with "replace"
*
*@param: find, a string, or an array of strings specfying what string(s) you want to find
*@param: replace, a string, or an array of string specifying what to replace the strings found with.
*if no value is specified, a empty string ("") replace value is assumed. If there are less entries in the replace array than in the find
*array, empty string values are assumes for the rest of the replace values. This value is optional.
*@param: limit, a limit to the amount of times to exacute the find & replace operation. This value is optional.
*@returns: the string after the find and replace operations have been completed
**/
String.prototype.findAndReplace = function(find, replace, limit) {
var tempString = this, tempParts, pos, loopLength;

if(typeof(find)!=="string") {//then the find and replace is an array
if(replace === undefined) replace = [""]; //if no replace was specified
else if(typeof(replace)==="string") replace = [replace]; //if only one replace was specified
} else {//then they are a string
//put both of them in array form
find = [find];
replace = [replace];
}

loopLength = find.length; //determines the loop length

for(var a = 0, c = 0; a<loopLength; a++, c = 0) {
while((pos = tempString.indexOf(find[a]))!==-1) {
if(limit!==undefined && c>=limit) break;

//split the array into 3 parts
tempParts = new Array(tempString.substring(0, pos), tempString.substr(pos, find[a].length), tempString.substr(pos+find[a].length));
tempParts[1] = (replace[a]===undefined)? ("") : replace[a]; //if there was no replace specified give an empty string
tempString = tempParts[0]+tempParts[1]+tempParts[2];

c++; //increase the counter
}
}

return tempString;
}

Johnny64
January 22nd, 2005, 01:36 PM
Hey

I know how to shorten down you code alot:

text_str = text_str.split("badword").join("****");

:)

Voetsjoeba
January 22nd, 2005, 02:36 PM
Yes, but that doesn't include the limit feature ;) You can split with a limit (split(needle, limit)), but then what it does is cut the entire string up and then take [limit] elements, instead of cutting it up in [limit] elements.

Johnny64
January 22nd, 2005, 03:45 PM
Yes, but that doesn't include the limit feature ;) You can split with a limit (split(needle, limit)), but then what it does is cut the entire string up and then take [limit] elements, instead of cutting it up in [limit] elements. only a small detail :P


.....but he has a point.....:hair:

iloveitaly
January 22nd, 2005, 05:25 PM
i could use the split feature and still have the limit feature, i would have to split() the string then splice() the array then join() one of the spliced arrays with the text i split off of the array. I'll just the indexOf(). :to:

Johnny64
January 22nd, 2005, 06:20 PM
i could use the split feature and still have the limit feature, i would have to split() the string then splice() the array then join() one of the spliced arrays with the text i split off of the array. I'll just the indexOf(). :to:
p00w3d Voets....muwhahahaha :P

.....sorry lost my self just there :D

Voetsjoeba
January 23rd, 2005, 08:36 AM
Heyheyhey, I never said it wasn't possible ! :P I only noted that with split().join() you don't have the limit feature, although it might seem that way because of the syntax (split(needle, limit)).

Btw, it's pwn3d ;)

senocular
January 23rd, 2005, 08:43 AM
its how n00bs spell it :P

Krilnon
January 24th, 2005, 06:03 PM
Thanks iloveitaly, the str_replace() PHP function you mentioned is something I can really use! I'd been looking for something like that for a while!