Remove characters from Strings in AS3
If you want to remove characters in a string, you can use the split and join properties in the String and Array Class. For example, to remove dashes from a string you can use the following.
var myText:String ="This-is-some-random-text";
var newString:String = myText.split("-").join(" ");
trace(newString); // This is some random text
The split property removes any characters specified inside the parameters. And the join property inserts a separator between the split elements. So, in the above example the separator is a space. Below is another example, but instead of joining using a space I have used a plus sign which will add a plus.var myText:String ="This-is-some-random-text";
var newString:String = myText.split("-").join("+");
trace(newString); // This+is+some+random+text
If you have multiple strings that you want to remove characters from, you can create a function to do the hard work.var number1:String = "000-111-222";
var number2:String = "333-444-555";
var number3:String = "666-777-888";
function thingToRemove(_text:String) {
var myText:String=_text;
var newString:String=myText.split("-").join(" ");
return newString;
}
trace(thingToRemove(number1)); //000 111 222
trace(thingToRemove(number2)); //333 444 555
trace(thingToRemove(number3)); //666 777 88811
You can also do the opposite and remove white spaces from a string instead of replacing it with a character.
var myText:String = “This is some more text”;
var newString:String = myText.split(" ").join("");
trace(myText); // Thisissomemoretext



0 comments:
Post a Comment