Recipe 12.6.
Parsing a String into Words
Problem
You want to process the individual words in a
string.
Solution
Use the split( ) method.
Discussion
The split( ) method, Recipe
5.6, splits a string into an array using the specified
delimiter. To split a string into words, use the split( )
method with a space as the delimiter.
// Create a string with multiple words.
var example:String = "This is a string of words";
// Split the string into an array of words using a space as the delimiter.
var words:Array = example.split( " " );
// Loop through the array and do something with each word.
// In this example, just output the values.
for ( var i:int = 0; i < words.length; i++ ) {
/* Displays:
this
is
a
string
of
words
*/
trace( words[i] );
}
You can process the individual words in many
ways. The following is a complete example that uses this technique
to split a string into words and then creates sprites containing
those words. The user can then drag the words around on stage to
form various sentences or statements similar to the popular
magnetic poetry kits:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class ActionScriptPoetry extends Sprite {
public function ActionScriptPoetry( ) {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
// Create a string, and split the string into an array of words.
var example:String = "This is a string of ActionScript poetry words";
var words:Array = example.split(" ");
var word:Sprite;
var wordText:TextField;
// Loop through all the words in the array.
for ( var i:int = 0; i < words.length; i++ ) {
// Create a new sprite for each word and add it to the
// display list so the sprite is drawn on-screen
word = new Sprite( );
addChild( word );
// Create a text field within the sprite by creating a new
// TextField instance and adding it as a child
wordText = new TextField( );
word.addChild( wordText );
// 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,
// and the text should not be selectable since we want to drag it around.
wordText.autoSize = TextFieldAutoSize.LEFT; // Left-justify the text
wordText.border = true;
wordText.background = true;
wordText.selectable = false;
// Set each text fields's text value to one of the words from the array.
wordText.text = words[i];
// The sprite is draggable when clicked, and it stops being
// draggable when released.
word.addEventListener( MouseEvent.MOUSE_DOWN, handleDrag );
word.addEventListener( MouseEvent.MOUSE_UP, handleDrop );
// Randomize the position of the sprites containing words
var rx:Number = Math.random( ) * stage.stageWidth - word.width;
var ry:Number = Math.random( ) * stage.stageHeight - word.height;
word.x = rx;
word.y = ry;
trace(word);
}
}
// This function is called when the uses presses a word with the mouse
private function handleDrag( event:MouseEvent ):void {
// The event target will be the TextField, so to get the
// word we work up to the parent
var word:Sprite = event.target.parent;
// Make the clicked on word draw on top of everything else
setChildIndex( word, numChildren - 1 );
// Drag the word around to coincide with mouse movement
word.startDrag( );
}
// This function is called when the user releases the mouse
private function handleDrop( event:MouseEvent ):void {
// The event target will be the TextField, the parent is the Sprite
var word:Sprite = event.target.parent;
// Stop moving the word around with the mouse
word.stopDrag( );
}
}
}
The preceding use of split( ) by itself
is all 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 use a regular expression as
the parameter to split( ) to remove them. The regular expression to match
punctuation around words should be constructed using the value
/[^a-zA-Z0-9]+/.
// Create a string that uses punctuation.
var example:String = "Here are some words. Also, here is some punctuation!";
// Create an array of words from the string without first removing punctuation.
var words:Array = example.split( ' ' );
// Display the elements of the array. Some of the elements also contain the
// punctuation. This is likely undesirable.
for ( var i:int = 0; i < words.length; i++ ) {
/* Outputs:
Here
are
some
words.
Also,
here
is
some
punctuation!
*/
trace( words[i] );
}
// Use a regular expression for punctuation and splits around that to produce
// a cleaned array of words from the string
words = example.split( /[^a-zA-Z0-9]+/ );
// Output all the elements of the words array. This time each element is a word,
// and none of the elements include punctuation.
for ( i = 0; i < words.length; i++ ) {
/* Outputs:
Here
are
some
words
Also
here
is
some
punctuation
*/
trace( words[i] );
}
Regular expressions, covered in Chapter
13, are a powerful new feature of ActionScript 3.0.
See Also
Chapter
13; Recipes 5.6
and 12.5
|