Tip #1: Using negative numbers with String operations

Every now and then you need to remove some characters from a String, and when is from the end of the String what I normally do is calculate the length of the String and subtract the amount of chars to remove.
for example:
1
2
var test:String = "This is a tip and trick";
trace (test.substr (18, test.length)); // Output: trick
But after checking the String Doc’s I found an alternative.
If endIndex is a negative number, the ending point is determined by counting back from the end of the string, where -1 is the last character.

So, with this we could do:

1
2
3
var test:String = "This is a tip and trick";
trace (test.slice(10, -10)); // Output: tip
trace (test.substr(-5)); // Output: trick

I don’t know if this is obvious for you but I haven’t tried this before…

Hope this can be useful
Gus

You can follow any responses to this entry through the RSS 2.0 feed.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>