Some String Utils Examples & Functions in ActionScript 3.0

memo Continue the articles which were published before, 7 Trim() Equivalent Function In Flash Might Help Someone Else, 16 Useful Mathematical Formulas In ActionScript 3 and 12 very simple,basic but useful function source in Flex, here is some string utils examples and functions in ActionScript 3.0. You can reference here for all the methods of the String class. *I think this article no help for most readers, only for AS beginers and the memo of myself, hope you don’t agree with me :)

Joining together strings


If you want to join strings together, the easiest way is to use the + sign, which will run two strings into each other.

trace("Hello " + " " + " world!"); //Traces: "Hello world!"
trace("How old are you? " + 20);//traces: "How old are you? 20"
trace(12 + 34);//Traces "46"
trace("" + 12 + 34);//traces: "1234"

 

Putting things in lower case or UPPER CASE


This is a relatively simple process, but is very useful when you are dealing with case sensitive stuff such as searches and replaces (which we will be covering later). You just use toLowerCase() or toUpperCase() as demonstrated here:

var example:String = "a mixture OF CASES";
trace(example.toLowerCase());//traces "a mixture of cases"
trace(example.toUpperCase());//traces "A MIXTURE OF CASES"
trace(example);//Hasn’t changed
//if you want it to:
example = example.toLowerCase();
trace(example);//traces "a mixture of cases"

 

Replacing within Strings


The new, AS3 only, quick and easy way to replace stuff within strings, which is terribly useful sometimes. The replace function takes only two arguments – what to find, and what to replace it with, like the Find: and Replace with: boxes in a certain well known word processer.

var example:String = "Foundation Flash is great";
trace(example.replace("great","even better than before"));
//Traces "Foundation Flash is even better than before"
trace(example);

 

Strip Html tags from the string


We needn’t build the search functionality that searches into an array containing sometimes html formatted text, the regular expression can perform it well in AS3.

/**
* 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),"" );
}

 

Check String is numeric or not


It’s simple with regular expression.

/**
* 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);
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply