Recipe 12.7.
Removing and Replacing Characters and Words
Problem
You want to remove characters from a string or
replace one substring with another.
Solution
Use the replace( ) method or split( ) and join( ) combination.
Discussion
ActionScript 3.0 includes a new method
String.replace( ), not
found in previous versions, that allows you to replace substrings
within a string. This method accepts two parameters:
pattern
-
The substring or regular expression you want to
find and replace.
replace
-
The value with which to replace each
pattern match. This value is typically a string but can
also be a function that returns a string. It is useful when
pattern is a regular expression.
There are two uses of this method based on the
parameters provided. This recipe focuses on using a string for the
pattern. A regular expression pattern also can be used, as covered
in Recipe
13.4.
Here is a simple example of replacing a
substring in a sentence. The replace( ) method returns a new
string with pattern replaced by replace, leaving
the original string unmodified.
var example:String = "This is a cool sentence."
// Replace " is " with " is not " in example.
// Displays: This is not a cool sentence.
trace( example.replace( " is ", " is not " ) );
In this example, the word "is" is surrounded by
spaces when used as the pattern. This is important because failing
to do so matches the "is" in "This" and the sentence starts with
"This not is," which is not the desired effect.
When a string is used for the pattern
in the replace( ) method, the method lacks functionality
compared to using a regular expression. The two biggest problems
are that only the first occurrence of pattern is replaced and you
have to roll your own solution for case-insensitive
replacement.
To replace all occurrences, you have to use a
combination of a loop and the replace method:
// Create a string with contractions, and another string to store the replacements
var example:String = "It's a bird, it's a plane, it's ActionScript Man!";
var replaced:String = example; // Initialize replaced with the original text
// As long as the pattern substring is found in replaced, we need to replace again
while ( replaced.indexOf( "it's" ) != -1 ) {
// Replace the first instance of "it's" with "it is".
replaced = replaced.replace( "it's", "it is" );
}
// To get around the case-sensitivity problem, we need to change the pattern and
// go through the replace process again. This fixes the uppercase "It's"
replaced = replaced.replace( "It's", "It is" );
// Outputs: It is a bird, it is a plane, it is ActionScript Man!
trace( replaced );
The split( ) method also can be used to
replace and remove characters and words in a string. Unlike using
replace( ) with a string pattern, split replaces all
occurrences of a word. However, both methods share the same
case-sensitivity problem. The
following is an example of replacing the HTML <br>
tag with the \\n character to properly break a sentence
into multiple lines:
var example:String = "This is<br>a sentence<br>on 3 lines";
// Remove the <br> tags and replace them with newline characters ('\n')
/* Display:
This is
a sentence
on 3 lines
*/
trace( example.split( "<br>" ).join( '\n' ) );
When a string is split, an array of strings is
returned. The Array.join( ) method can then be used to build
a new string out of the individual array elements. Splitting
removes the delimiter, allowing us to join the string again with a
new delimiter passed to the join( ) method. This technique
replaces all occurrences of the delimiter passed to split.
To remove characters or words instead of
replacing them simply use the empty string as the replacement
string:
var example:String = "This is a cool sentence.";
// Remove the word "cool" and it's trailing space from example
// Displays: This is a sentence.
trace( example.replace( "cool ", "" ) );
See Also
Recipes
12.4
and 12.7
|