# \[fmx\] Substituting characters from strings

**URL:** <https://forum.kirupa.com/t/fmx-substituting-characters-from-strings/56866>\
**Category:** flash\
**Created:** [July 8, 2004, 9:33am UTC](https://forum.kirupa.com/t/fmx-substituting-characters-from-strings/56866 "2004-07-08T09:33:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![borispc](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@borispc](https://forum.kirupa.com/u/borispc)\
**Post date:** [July 8, 2004, 9:33am UTC](https://forum.kirupa.com/t/fmx-substituting-characters-from-strings/56866/1 "2004-07-08T09:33:00Z")

</div>

Hello!

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 10:58am UTC](https://forum.kirupa.com/t/fmx-substituting-characters-from-strings/56866/2 "2004-07-08T10:58:12Z")

</div>

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

```auto
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;
	trace(my_str);
} else trace("Finished.");

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 11:47am UTC](https://forum.kirupa.com/t/fmx-substituting-characters-from-strings/56866/3 "2004-07-08T11:47:18Z")

</div>

[http://www.kirupaforum.com/forums/showthread.php?t=9235](http://www.kirupaforum.com/forums/showthread.php?t=9235)
