A useful prototype for running a ’search and replace’ on a string in Flash. Great, for example, when a database is spewing out rubbish string pre/post fixes, or HTML code that Flash doesn’t support. Im sure there is similar stuff out there but I hope it will be useful for someone none-the-less.

Check it out…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
String.prototype.replace = function (oldCrap, goodStuff)
{
	var tempShit = "";
	var i = 0;
	if (oldCrap == "")
	{
		return this;
	}
	if (this.indexOf (oldCrap) != -1)
	{
		while ((bla = this.indexOf (oldCrap, i)) != -1)
		{
			tempShit += this.substring (i, bla);
			tempShit += goodStuff;
			i = bla + oldCrap.length;
		}
		return tempShit + this.substring (i);
	}
	else
	{
		return this;
	}
};
 
/* EXAMPLE USAGE: */
 
var oldString = "George W Bush is great";
var newString:String = oldString.replace ("great", "mentally defficient");
 
trace (newString);