How to delete part of a string in actionscript?

Rella picture Rella · Mar 20, 2010 · Viewed 11k times · Source

So my string is something like "BlaBlaBlaDDDaaa2aaa345" I want to get rid of its sub string which is "BlaBlaBlaDDD" so the result of operation will be a string "aaa2aaa345" How to perform such thing with actionscript?

Answer

Lance Pollard picture Lance Pollard · Mar 20, 2010

I'd just use the String#replace method with a regular expression:

var string:String = "BlaBlaBlaDDD12345";
var newString:String = string.replace(/[a-zA-Z]+/, ""); // "12345"

That would remove all word characters. If you're looking for more complex regular expressions, I'd mess around with the online Rubular regular expression tester.

This would remove all non-digit characters:

var newString:String = string.replace(/[^\d]+/, ""); // "12345"

If you know the exact string you want to remove, then just do this:

var newString:String = string.replace("BlaBlaBlaDDD", "");

If you have a list (array) of substrings you want to remove, just loop through them and call the string.replace method for each.