DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 9.9 Removing and Replacing Characters

9.9.1 Problem

You want to remove characters from a string and optionally replace them.

9.9.2 Solution

Create a custom String.simpleReplace( ) method. Alternatively, for replacing patterns, use the String.replace( ) method included in RegExp.as.

9.9.3 Discussion

ActionScript does not provide a native method that replaces substrings within a string. Therefore, you must use a custom method to do so. If you want to replace instances of a specific substring, you can create a custom simpleReplace( ) method for the String class. This method should accept up to three parameters:

search

The substring you want to find and replace.

replace

The value with which to replace the occurrences of the search substring.

matchCase

If true, the method performs a case-sensitive search. Otherwise, it performs a case-insensitive search.

Here is our custom String.simpleReplace( ) method:

String.prototype.simpleReplace = function (search, replace, working) {

  // temp stores the string value with the replaced substrings.
  var temp;

  // working holds the value of the string.
  var working = this;

  // Perform a case-insensitive search if so directed.
  if (!matchCase) {
    working = this.toLowerCase(  );
    search = search.toLowerCase(  );
  }

  // searchIndex holds the starting index of a matches. startIndex stores the value
  // of the index after the replaced substring.
  var searchIndex = -1;
  var startIndex = 0;

  // Find each match to the search substring.
  while ((searchIndex = working.indexOf(search, startIndex)) != -1) {

    // Append to temp the string value from the end of the last match to just before
    // the current match. Then, append the replace string in place of the search
    // substring.
    temp += this.substring(startIndex, searchIndex);
    temp += replace;

    // startIndex holds the index one after the final character of the matched
    // substring. This starts the next search-and-replace operation after the
    // substring that is being replaced.
    startIndex = searchIndex + search.length;
  }

  // Return the temp value plus the remainder of the original 
  //string value (after the last match).
  return temp + this.substring(startIndex);
};

// Create a string to test.
myString = "It's a bird, it's a plane, it's ActionScript Man!";

// Replace all instances of "it's" with "it is". Perform a case-insensitive match.
// Outputs: it is a bird, it is a plane, it is ActionScript Man!
trace(myString.simpleReplace("it's", "it is", false));

In cases where you need to perform pattern matching as part of the replacement within a string, you should use the custom String.replace( ) method, which is available only if you include the RegExp.as file. This method allows you to match and replace using a regular expression instead of a string literal, which offers a great deal more flexibility.

You should first create a regular expression using the RegExp constructor. The regular expression should be the pattern that you want to match and replace in your string. Then pass that regular expression as the first parameter to the String.replace( ) method. The second parameter should be a string literal that you want to use to replace all occurrences of the pattern.

// You must include the third-party RegExp.as file from Recipe 9.6.
#include "RegExp.as"

// Create a string that includes people and telephone numbers. The telephone numbers
// follow the same pattern, so you can use a regular expression to replace them.
myString = "Regina [555-1212], Henriette [555-1234]";

// Create a regular expression that matches the telephone number pattern and searches
// globally. This pattern matches U.S. telephone number patterns (without area codes)
// and is provided for demo purposes only. You must adapt it to match telephone
// number patterns in your country. You could also use "\\d" instead of "[0-9]" in
// the following expression.
re = new RegExp("[0-9]{3}-[0-9]{4}", "g");

// Output the string with the telephone numbers replaced by X's. The result is:
// Regina [XXX-XXXX], Henriette [XXX-XXXX]
trace(myString.replace(re, "XXX-XXXX"));
    [ Team LiB ] Previous Section Next Section