Recipe 9.8 Parsing a String into Words

9.8.1 Problem

You want to process a string one word at a time.

9.8.2 Solution

Use the split( ) method.

9.8.3 Discussion

The split( ) method (see Recipe 6.6) splits a string into an array using the specified delimiter. To split a string into separate words, use the split( ) method with a space as the delimiter:

// Create a string with multiple words. myString = "this is a string of words"; // Split the string into an array of words using a space as the delimiter. words = myString.split(" "); // Loop through the array and do something with each word. In this example, we just // output the values. for (var i = 0; i < words.length; i++) {   /* Displays:      this      is      a      string      of      words   */   trace(words[i]); }

You can process the individual words in many ways. Here is an example that uses this technique to split a string into words and then creates movie clips containing those words. The user can then drag the words around on stage to form various sentences or statements, as in the popular magnetic poetry kits.

// Create a string and split the string into an array of words. myString = "This is a string of ActionScript poetry words"; words = myString.split(" "); // Loop through all the words in the array. for (var i = 0; i < words.length; i++) {   // Create a new movie clip for each word.   word_mc = _root.createEmptyMovieClip("word" + i, i);   // Create a text field within each movie clip.   word_mc.createTextField("word_txt", 1, 0, 0, 0, 0);     // The text field should autosize to fit its contents. It should also have a border   // and background so that it mimics the look of poetry magnets.   word_mc.word_txt.autoSize   = true;   word_mc.word_txt.border     = true;   word_mc.word_txt.background = true;   // Set each movie clip's text field value to one of the words from the array.   word_mc.word_txt.text = words[i]   // The movie clip is draggable when clicked and stops    // being draggable when released.   word_mc.onPress = function (  ) {     this.startDrag(  );   };   word_mc.onRelease = function (  ) {     this.stopDrag(  );   };   // Randomize the position of the movie clips containing words.   rx = Math.random(  ) * Stage.width  - word_mc._width;   ry = Math.random(  ) * Stage.height - word_mc._height;   word_mc._x = rx;   word_mc._y = ry; }

The preceding use of split( ) by itself is all that you need when the original string value contains words and spaces but no punctuation. If the string has punctuation or other miscellaneous characters, you should remove them first by using a regular expression and the custom String.replace( ) method included in RegExp.as. The regular expression to remove everything except letters, numbers, and spaces is [^a-zA-Z0-9 ]:

// You must include the third-party RegExp.as file from Recipe 9.6.  #include "RegExp.as" // Create a string that uses punctuation. myString = "Here are some words. Also, here is some punctuation!"; // Create an array of words from the string without first removing punctuation. words = myString.split(" "); // Display the elements of the array. Some of the elements also contain  // punctuation. This is most likely undesirable. for (var i = 0; i < words.length; i++) {   /* Outputs:      Here      are      some      words.      Also,      here      is      some      punctuation!   */   trace(words[i]); } // Create a regular expression that can be used to remove all nonalphanumeric // characters and spaces. re = new RegExp("[^a-zA-Z0-9 ]", "g"); // Call the replace(  ) method of myString. Pass it the regular expression, re, and // tell it to replace all nonmatching characters with the empty string. nString = myString.replace(re, ""); // Split nString, the result of the replace(  ) method call, into an array of words. words = nString.split(" "); // Output all the elements of the words array. This time each element is a word, and // none of the elements include punctuation. for (var i = 0; i < words.length; i++) {   /* Outputs:      Here      are      some      words      Also      here      is      some      punctuation   */   trace(words[i]); }

Including the RegExp.as file also enhances the String.split( ) method to support regular expressions as a parameter directly. Therefore, instead of calling String.replace( ) separately, as shown in the preceding example:

re = new RegExp("[^a-zA-Z0-9 ]", "g"); nString = myString.replace(re, ""); words = nString.split(" ");

you can pass the RegExp object directly to split( ), as follows:

re = new RegExp("[^a-zA-Z0-9]", "g"); words = myString.split(re);

9.8.4 See Also

Recipe 6.6, Recipe 9.6, Recipe 9.5, and Recipe 9.9



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net