14
Nov
08

String Utils in As3

I tried to use the power of Regex in AS3 and came with this StringUtils Class.
Some of the complex String manipulations can be done easily using Regex in AS3. We have discussed a bit about Regex in Introduction to AS3 Part1

We shall discuss brief about Regex in another tutorial.

Some of the String manipulations that can be done using this StringUtils Class are

 

Trim:

  Trim/Strip unwanted spaces from the given string.
  Syntax: Trim( String, Boolean = true) : String;
  If second @param is false Trim method will not remove the unwanted spaces in the String, But will trim the left and right spaces.

 StripHtmlTags:

  Strip Html tags from the string.
  Syntax: StripHtmlTags( String ) : String;  

 IsNumeric:

  Checks and returns Boolean whether given String is numeric or not.
  Syntax: IsNumeric( String ) : Boolean;

 RemoveDuplicateWords:

  Removes the repeated words from the given String.
  Syntax: IsNumeric( String ) : Boolean; 

 
 CapitalizeFirstLetters:

  Capitalizes the first character of the words in the given String.
 Syntax: CapitalizeFirstLetters( String , Boolean ) : Boolean;  
  If second @param is false CapitalizeFirstLetters method will Capitalize all the letters.

 

 
Code:


/**
 *  User StringUtils
 *  author: Saravanan
 *  version: 1.0
 *  modified: 08/20/2008
 *  copyright: Designscripting.com
 *
 *  This code defines a custom StringUtils that allows you to do following StringManipulations.
 * Trim:
 *  Trim/Strip unwanted spaces from the string.
 *  Syntax: Trim( String, Boolean = true) : String;
 *  If second @param is false Trim method will not remove the unwanted spaces in the String, But will trim the left and right spaces.
 *  
 * StripHtmlTags:
 *  Strip Html tags from the string.
 *  Syntax: StripHtmlTags( String ) : String;  
 *  
 * IsNumeric:
 *  Checks and returns Boolean whether given String is numeric or not.
 *  Syntax: IsNumeric( String ) : Boolean;
 *  
 * RemoveDuplicateWords:
 *  Removes the repeated words from the given String.
 *  Syntax: IsNumeric( String ) : Boolean;  
 *  
 * CapitalizeFirstLetters:
 *  Capitalizes the first character of the words in the given String.
 *  Syntax: CapitalizeFirstLetters( String , Boolean ) : Boolean;  
 *  If second @param is false CapitalizeFirstLetters method will Capitalize all the letters.
 *  
**/

package com.designscripting.Utils
{
 public class StringUtils
 {
  /**
   Constructor Function
  */
  public function StringUtils()
  {}
  /**
   * Trim/Strip unwanted spaces from the string.
   *
   * @param String  String to be trimmed
   * @param Boolean  default is true, if false is used will do only LTrim and RTrim.
   * @return    Trimmed String
   */
  public static function Trim( inputStr : String, extraWhiteSpace : Boolean = true ) : String
  {
   var temp : String = inputStr;
   var obj : RegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if ( obj.test(temp) )
   temp = temp.replace(obj, '$2');
   if( extraWhiteSpace )
   {
      var obj1 : RegExp = / +/g;
      temp = temp.replace( obj1, " " );
      if ( temp == " " )
      temp = "";
    }
     return temp;
  }
  /**
   * Strip Html tags from the string.
   *
   * @param String  String to be Strip
   * @return    Clear String with no tags
   */
  public static function StripHtmlTags( inputStr : String ) : String
  {
   return inputStr.replace(RegExp(/<.*?>/g),"" );
  }
  /**
   * Checks and returns Boolean whether given String is numeric or not.
   *
   * @param String  String contains numeric data
   * @return    Boolean.
   */
  public static function IsNumeric( inputStr : String ) : Boolean
  {
   var obj:RegExp = /^(0|[1-9][0-9]*)$/;
   return obj.test(inputStr);
  }
  public static function RemoveDuplicateWords( inputStr : String ) : String
  {
   var obj:RegExp = new RegExp("\\b (?<word>[a-z]+) \\s+ \\k<word> \\b", "gix")
   return Trim(inputStr.replace(obj, "" ), true);
  }
  /**
   * Capitalizes the first character of the words in the given String.
   *
   * @param String  String to be Capitalized
   * @param Boolean  default is true, if false is used will capitalize all the chars.
   * @return    Capitalized String
   */
  public static function CapitalizeFirstLetters( inputStr:String, allChars : Boolean = true ) : String
  {
   
   var str:String;
   if( allChars )
   {
    var outputStr:String = "";
    var arr : Array = inputStr.split(" ");
    var len : Number = arr.length;
    for(var j : int = 0; j< len; j++){
    str = arr[j].substr(0,1)
    outputStr += str.toUpperCase()+arr[j].substr(1)+" ";
    }
    return Trim(outputStr);
   }
   return inputStr.toUpperCase();
  }
 }
}

Usage of  that code in Fla:
import com.designscripting.StringUtils;
trace("Trim all the spaces-->"+StringUtils.Trim(" df f   f  f  f      f    f  f  ")+"--");
trace("Only LTrim and RTrim-->"+StringUtils.Trim(" df f   f  f  f      f    f  f  ", false)+"--");
trace("StripHtmlTags-->"+StringUtils.StripHtmlTags("<html><a href='dffsdf/dfdf'>here</a> data here</html>")+"--");
trace("Check IsNumeric-->"+StringUtils.IsNumeric("3746734")+"--");
trace("Check CapitalizeFirstLetters-->"+StringUtils.CapitalizeFirstLetters("word word word")+"--");
trace("Check CapitalizeAllLetters-->"+StringUtils.CapitalizeFirstLetters("word word word", false)+"--");

As the methods are static we should use the Class.Method name to call.
In other words we should not use Object.Method name to call static methods.

For example:
StringUtils.Trim( String ); where string is the input string value.
 
Here StringUtils is the Class name and Trim is the Static method.

Download Source files

Share/Save/Bookmark

Related posts:

  1. Search and Replace String AS2/AS3:
  2. XMLManager in ActionScript 3.0
  3. Calculating fileSize / converting loaded bytes


5 Responses to “String Utils in As3”


  1. 1 Jerry Feb 6th, 2009 at 7:09 pm

    Cool, great Class! I’ll check it out and twitter it! Nice!!

  2. 2 tristian Mar 4th, 2009 at 11:44 pm

    Do you know if there is anyway to take a RegExp from a texfield and use that as the regular expression?
    ie rather as in your example use a drop down of pre-defined patterns, but allow a custom pattern to be created.

  3. 3 admin Mar 5th, 2009 at 7:10 pm

    Hi Tristian,

    If you want to take the regex from textfield…

    Just pass the expression
    var obj : RegExp = textfield.text;

    -sara

  4. 4 Tom Apr 29th, 2009 at 2:24 am

    Just a note, in Flash IDE you apparently have to wrap the RE string in an actual new RegExp() call (I know, I know, IDE sucks but I have to for this project):

    var obj:RegExp=new RegExp( …regex goes here … );

  5. 5 tj May 24th, 2009 at 10:58 pm

    Is this right? Your import line doesn’t match the location of the file as it is in a ‘Utils’ folder. Your import doesn’t mention it.

Leave a Reply