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
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