Recipe 12.8. Retrieving One Character at a Time


Problem

You want to retrieve one character at a time from a string.

Solution

Use a for statement and the String.charAt( ) method. Alternatively, use String.split( ) with the empty string as the delimiter to split the string into an array of all the characters, and then use a for statement to loop through the array.

Discussion

The simplest way to extract each character of a string is to use a for statement that loops over all of the character positions in the string from index zero to string.length 1, incrementing by one on each iteration. Within the for statement body, you can use the charAt( ) method to extract the character for processing.

var example:String = "a string"; // Loop over all of the chatacters of the string. for ( var i:int = 0; i < example.length; i++ ) {   /* Output each character, one at a time. This displays:      a      s      t      r      i      n      g   */   trace( example.charAt( i ) ); }

You can achieve the same effect by using the split( ) method to first split the string into an array of characters, and then looping through the array to process each character. Use the empty string as the delimiter parameter for the split( ) method to break between each character.

var example:String = "a string"; // Split the string into an array of characters (one-character strings). var characters:Array = example.split( "" ); // Loop through all the elements of the characters array. for ( var i:int = 0; i < characters.length; i++ ) {   /* Output each character element. This displays:      a      s      t      r      i      n      g   */   trace( characters[i] ); }

Both techniques are generally interchangeable, though the second one offers some advantages if you want to work with the characters by using common array methods. For example, if you first split a string into an array of characters, you can sort that array. This is not as easily done when you use the charAt( ) technique:

var example:String = "a string"; var characters:Array = example.split( "" ); // Alphabetically sort the array of characters. characters.sort(  ); for ( var i:int = 0; i < characters.length; i++) {   /* Displays:            a      g      i       n      r      s      t   */   trace( characters[i] ); }

Also, if you want to use this process to remove every instance of a particular character, it is easier with an array than with the charAt( ) technique:

var example:String = "a string"; var characters:Array = example.split( "" ); for ( var i:Number = 0; i < characters.length; i++ ) {   // Remove all "r" elements from the array. Be sure to decrement i if an   // element is removed; otherwise, the next element is improperly skipped.   if ( characters[i] == "r") {     characters.splice( i, 1 );     i--;   } } // Displays: a sting trace( characters.join( "" ) );

Although the preceding technique for replacing characters works for simple cases, you should see Recipe 12.7 for more capable alternatives.

See Also

Recipe 12.7




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

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