# \[AS2\] Simple Find And Replace

**URL:** https://forum.kirupa.com/t/as2-simple-find-and-replace/214968
**Category:** open source
**Created:** [February 2, 2007, 2:23pm UTC](https://forum.kirupa.com/t/as2-simple-find-and-replace/214968 "2007-02-02T14:23:31Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)
#### Post date: [February 2, 2007, 2:23pm UTC](https://forum.kirupa.com/t/as2-simple-find-and-replace/214968/1 "2007-02-02T14:23:31Z")

</div>

I had a minute so I whipped up this;

**[SIZE=4]String.replace()[/SIZE]**  
string.slice(replaceFrom:String, replaceTo:String\*[, caseSensitive:Boolean]\*) : String

Returns a string that replaces all substrings of a string within a main string. The original String object is not modified. If the end parameter is not specified, the end of the substring is the end of the string. If the caseSensitive[FONT=Verdana] parameter is not specified[/FONT] it is read as [FONT=Courier New]false[/FONT][FONT=Verdana], and case sensitivity is applied.[/FONT]

**Parameters**

**replaceFrom** :String - The string that will be replaced.  
**replaceTo** :String - What all instances of [FONT=Courier New]replaceFrom [/FONT][FONT=Verdana]will be replaced to.[/FONT]  
[FONT=Verdana] **caseSensitive** :Boolean - Whether or not all instances of [FONT=Courier New]replaceFrom [/FONT][FONT=Verdana]will be replaced to [FONT=Courier New]replaceTo[/FONT], regardless of it’s case. Default is [FONT=Courier New]true[/FONT].[/FONT][/FONT]  
**Returns**

String - A whole string of the specified string with all instances of [FONT=Courier New]replaceFrom [FONT=Verdana]replaced with[/FONT] replaceTo[/FONT].  
**Example**

The following example creates a variable, string, assigns it a String value, and then calls the replace() method using a variety of values for the replaceFrom[FONT=Verdana], [/FONT]replaceTo and caseSensitive parameters. Each call to replace() is wrapped in a trace() statement that displays the output in the Output panel.

```auto
var string:String = "An Apple a day KeePs The Doctor away"
String.prototype.replace = function(replaceFrom:String, replaceTo:String, caseSensitive:Boolean):String {
 var start:Array = this.split(replaceFrom);
 tmp = start.join(replaceTo);
 if (!caseSensitive) {
  var start:Array = tmp.split(replaceFrom.toLowerCase());
  tmp = start.join(replaceTo);
  var start:Array = tmp.split(replaceFrom.toUpperCase());
  tmp = start.join(replaceTo);
 }
 return tmp;
};
// Replace all instances of "Apple" in string with "n Slice Of Cake"
// Case sensitive is not specified, it will be read assumed true
trace(string.replace("n Apple", " Slice Of Cake"));
// Returns "A Slice Of Cake a day KeePs The Doctor away"
 
// Replace all instances of "a" in string with "b"
// Case sensitive is specified as true
trace(string.replace("a", "b", true));
// Returns "An Apple b dby KeePs The Doctor bwby"
 
// Replace all instances of "a" in string with "b"
// Case sensitive is specified as false
trace(string.replace("a", "b", false));
// Returns "bn bpple b dby KeePs The Doctor bwby"

```

EDIT: Had the case sensitivy around the wrong way around
