Groups | Blog | Home
all groups > flash data integration > december 2005 >

flash data integration : Is there a way to replace characters in a string/array


alfaro_vive
12/2/2005 12:00:00 AM
I am trying to find a function similar to php's str_replace is there something that will allow me to replace a certain character in a variable?

thanks for the help.
Masamune
12/2/2005 12:00:00 AM
There isn't a built-in function like str_replace, but I use the following
function that I made:



function str_replace(search_char:String, replace_char:String,
subject_string:String):String {
var new_string:String = "";
for (k=0; subject_string.length>k; k++) {
if (subject_string.charAt(k) == search_char) {
new_string = new_string+replace_char;
} else {
new_string = new_string+subject_string.charAt(k);
}
}
return new_string;
}
Jeckyl
12/2/2005 12:00:00 AM
[quoted text, click to view]

There is a pair of built in functions

newString = oldString.split(searchSrting).join(replaceString);

That will be a LOT faster than you writing code to do it.
--
Jeckyl

Masamune
12/2/2005 1:34:04 PM
Jeckyl's solution is not only much faster, but a lot more versatile. You can
use it to replace not only certain characters, but also words or character
patterns. Quite useful.

Thanks for this, Jeckyl.

Chris
Jeckyl
12/5/2005 9:10:25 AM
you're welcome.

tomeanand NO[at]SPAM hotmail.com
12/6/2005 12:22:46 PM
Hi all

I just changed a bit in Jeckyl's solution
You can give an array of strings that has to be removed and replaced.
Only thing you need to be carefull is the index of the elements in the arrays
[oldStr and newStr] should be same

//in param_str parameter give the sentence
function processText(param_str:String):String {
var itemsRemove = ["removeItem_01","removeItem_02","removeItem_03"];//
provide the words letters has to be moved
var itemsReplace = ["replaceItem_01","replaceItem_02","replaceItem_03"]; //
provide the words letters has to be replaced
var ret_str = param_str;
for (var i = 0; i<itemsRemove.length; i++) {
ret_str = (ret_str.split(itemsRemove)).join(itemsReplace);
}
return ret_str;
}

Anand
AddThis Social Bookmark Button