PDA

View Full Version : [AS2] Simple Find And Replace



nathan99
February 2nd, 2007, 09:23 AM
I had a minute so I whipped up this;

String.replace()
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 parameter is not specified it is read as false, and case sensitivity is applied.

Parameters

replaceFrom:String - The string that will be replaced.
replaceTo:String - What all instances of replaceFrom will be replaced to.
caseSensitive:Boolean - Whether or not all instances of replaceFrom will be replaced to replaceTo, regardless of it's case. Default is true.
Returns

String - A whole string of the specified string with all instances of replaceFrom replaced with replaceTo.
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, replaceTo and caseSensitive parameters. Each call to replace() is wrapped in a trace() statement that displays the output in the Output panel.

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

suicidePills
February 2nd, 2007, 11:28 AM
That's a pretty neat little method. Is that really AS2 though? I thought that AS2 was purely a class based system... Maybe I'm just confused. Maybe I've just been using AS3 for too long...:crazy:

nathan99
February 2nd, 2007, 06:21 PM
code fixed; no recursion worries+faster

ajcates
February 3rd, 2007, 11:58 AM
you should set up a default vaule for caseSensitive, and i would think the natural default value for it would be false, all other words nice little script.

also nice documention on the code

/me takes the script saves it in useful action scripts file

icio
February 3rd, 2007, 02:39 PM
If you're looking for a quick script to find and replace all instances of a piece of text in a string (case sensetive) you can use this code:

String.prototype.replace = function(find:String, replace:String) :String {
return this.split(find).join(replace);
}

I think that's right, anyway - haven't tested it :)

SuyabCo
February 4th, 2007, 10:20 AM
I have a hard question....


Just for example, if Im making an RPG, say 10 characters to choose from, but you have to play through on the first character you select... Would that be a find and replace situation? And is there a 'swap code' function, so the character you select would not have to meet himself?

Would it be easier, to have a world with every character allready in that world?

And I'll go deeper than that...
What if you character starts off in a town right, and all the people in that village are 'friendlys'. How would I go about having different 'clans' and so forth?

Say like:

password = inputName

if (password == "Being_1") {
answer = "Access granted!" ; _root.gotoAndStop("Level_1")
} else {
answer = "Access denied!" ;
}

Then for another character like 5 levels away:

password = inputName

if (password == "Being_2") {
answer = "Access granted!" ; _root.gotoAndStop("Level_5")
} else {
answer = "Access denied!" ;
}

I know I need to add something... Link it to the symbol, and have a flase,true, variable thing?

SuyabCo
February 5th, 2007, 03:08 AM
Any clues??? Help?