[fmx] Substituting characters from strings


I’d like to know how to substitute characters of strings.
Let’s say I have…

my_str=“Hello, Johny!, hello Dolly!”;

I want to substitute all “!” characters to “?”. How do we do this?

I am assuming that first we need to know the character position of the character we want to get rid of.

(we would loop this whole 3 step process until no more “!” characters were found in the string)

  1. my_str.indexOf("!"); returns 12
    [COLOR=DarkOrange]2) Now…how do I delete that character?[/COLOR]
  2. The next step from there would probably be to use one of the methods String.substring() or String.slice to introduce the new character “?”;

What’s the best way to do this?

This is probably a very bad way to do it, but it works…

my_str = "Hello, Johny!, hello Dolly!";
i = 0;
while (my_str.indexOf("!", i) != -1) {
	s = my_str.indexOf("!", i);
	s1 = my_str.substr(0, s);
	s2 = "?";
	s3 = my_str.substr(s+1, my_str.length);
	i = s+1;
	my_str = s1+s2+s3;
} else trace("Finished.");