Recipe 12.10. Trimming Whitespace


Problem

You want to trim the whitespace from the beginning and end of a string.

Solution

Use the custom ascb.util.StringUtilities.trim( ) method. Alternatively, if you are using the Flex 2 framework, you can use the mx.utils.StringUtil.trim( ) static method.

Discussion

Extra whitespace at the beginning and end of a string is a common enough annoyance that you should have a way of dealing with it. ActionScript does not provide a native trim( ) implementation, so you have to either write your own or use a pre-coded solution.

The basic steps involved are as follows.

  1. Split the string into an array of characters.

  2. Remove whitespace elements at the beginning of the array until there is not a whitespace character (tab, form feed, carriage return, newline, or space).

  3. Remove whitespace elements at the end of the array.

  4. Use join( ) to form the array characters into a single string and return that value.

The ascb.util.StringUtilities.trim( ) method works as described in the preceding steps. The following is the actual definition of the method, along with the isWhitespace( ) helper method:

// Returns true if the character is a whitespace character public static function isWhitespace( ch:String ):Boolean {   return ch == '\r' ||           ch == '\n' ||          ch == '\f' ||           ch == '\t' ||          ch == ' ';  } public static function trim( original:String ):String {   // Split the string into an array of characters.   var characters:Array = original.split( "" );   // Remove any whitespace elements from the beginning of the array using   // splice(  ). Use a break statement to exit the loop when you reach a   // non-whitespace character to prevent it from removing whitespace   // in the middle of the string.   for ( var i:int = 0; i < characters.length; i++ ) {     if ( isWhitespace( characters[i] ) ) {       characters.splice( i, 1 );       i--;     } else {       break;     }   }   // Loop backward through the array removing whitespace elements until a   // non-whitespace character is encountered. Then break out of the loop.   for ( i = characters.length - 1; i >= 0; i-- ) {     if ( isWhitespace( characters[i] ) ) {       characters.splice( i, 1 );     } else {       break;     }   }   // Recreate the string with the join(  ) method and return the result.   return characters.join(""); }

The following illustrates how you can use the trim( ) method.

// Create a string with beginning and ending whitespace. var example:String = "\n\r\f\ta string\t\t\n\n"; /* Display the value before calling the trim(  ) method. Displays: this string value is:      a string         <end> */ trace( "this string value is: " + example + "<end>" ); // Set example to the value returned by the trim(  ) method. example = StringUtilities.trim( example ); // Now, display the value again using the same trace(  ) statement.  //Displays: this string value is: a string<end> trace( "this string value is: " + example + "<end>" );

The Flex 2 framework implementation of trim is almost exactly the same. Here is some example code for that:

// Displays: a string<end> trace( StringUtil.trim( "\n  \r\ta string\t\t\n\n" ) + "<end>" );




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