# String searchAndReplace()

**URL:** <https://forum.kirupa.com/t/string-searchandreplace/203668>\
**Category:** open source\
**Created:** [October 12, 2006, 10:20pm UTC](https://forum.kirupa.com/t/string-searchandreplace/203668 "2006-10-12T22:20:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mofongo9](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mofongo9](https://forum.kirupa.com/u/mofongo9)\
**Post date:** [October 12, 2006, 10:20pm UTC](https://forum.kirupa.com/t/string-searchandreplace/203668/1 "2006-10-12T22:20:52Z")

</div>

one thing that i always thought was missing in the String class was a searchAndReplace function, and maybe there is one, but i have not heard about it at all, so i thought i’d make my own function.

since i didnt want to mess with the String class, i couldnt use String[dot]function(), so i have to reference the string as a parameter  
plus, right after you reference the function, you have to say  
[insert my string name here]=new\_str

i still think its pretty okay, though

```auto
function searchAndReplace(oldchar:String, newchar:String, target:String) {
    new_str = "";
    for (i=0; i<target.length; i++) {
        if (target.charAt(i) == oldchar) {
            new_str += newchar;
        } else {
            new_str += target.charAt(i);
        }
    }
    target = new_str;
}
var my_string:String = "Replace every lower case E in this sentence with the letter 'a'";
searchAndReplace("e", "a", my_string);
Astring = new_str

```

```auto
http://img61.imageshack.us/my.php?image=searchandreplaceml8.swf

```
