Tip #1: Using negative numbers with String operations
Posted by Gus on August 18th, 2010 | 0 comments
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