Actionscript 3.0 — String

Here I am talking about some tricky things about “String” class in actionscript 3.0:

We all know about the the operator like “+”, “+=” “<” “<=” “>” and “>=”. How actually they work in actionsript? I will give several examples;

when “+” and “+=” are used as string operator, they will treat their object as “Sting”, no matter what originally they are.

var a:String=”ss”;

var b:int=3;

var c:String=a+b;

trace(c) ;//—-ss3

var d:Boolean=true;

c+=d;

trace(c)//—-ss3true;

here you can see, all the variables before and after “+” or “+=” are treated as “string”

The difference between “substring()”,”slice()” and “substr()”;

All of those three methods are used to get short string from the longer one, the original string will not be changed after operation. Following are their calling function:

orgString.substring(beginIndex, endIndex);

orgString.slice(beginIndex, endIndex);

orgString.substr(beginIndex, length);

the substring method and slice(beginIndex, endIndex) look like similar at first glance, however, they are different at following two fields:

  1. Negative Index — The beginIndex and endIndex in substring() cannot be negative. So the result for orgString.substring(-1,-4) will be empty. While you can use negative index in slice(); -1 means the last charactor. For example:
    var org:String=”asdfg”;
    trace(org.slice(-3,-1));//——-df
  2. When the beginIndex is larger than endIndex; In substring(), the function will automatically swatch the index, for example: substring(2,0)=substring(0,2); while in slice(), the method return an emptry string. That means if you call trace(org.slice(-1,-3)) in example in list 1, it will return you nothing.

for substr() function, the beginIndex can be positive or negative, if it is negative, counting from right to left. The length can never be negative, otherwise, you will get nothing.

Share this post:

Leave a Comment.