Home » ActionScript 3.0

String Utils in As3

14 November 2008 One Comment
Share on Facebook

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

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


  • Bredan Owens

    Nice. I believe, when using the CapitalizeFirstLetters you should first change the whole string to lowercase otherwise, if the user enters something in all capitals, athough it capitalizes the first letter, redundantly, all the other letters are also capitals and it does not achieve the desired result.

    But surely as3 has built in functions for all these manipulations no?