Ted Patrick - Demos & MAX @ Adobe Systems


Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



Using String.slice with negative numbers

DIGG IT!     7 Comments Published Wednesday, March 05, 2008 at 8:11 PM .

If you find yourself working with data, you are bound to want to get a slice of a String. Most developers are aware of using positive integers with String.slice but I keep finding many are unfamiliar with using negative numbers.

Here are some examples:

var str:String = "Hello from Paris, Texas!!!";
trace( str.slice( 11 , 15 ) ); // output: Pari
trace( str.slice( -3 , -1 ) ); // output: !!
trace( str.slice( -3 , 26 ) ); // output: !!!
trace( str.slice(-3 , str.length ) ); // output: !!!
trace( str.slice( -8 , -3 ) ); // output: Texas


I find that slice is most handy because you do not need a length property, the length of the string is inferred when a negative index is used.

Truth be told I am working on IFBIN 2.0 on Adobe AIR and found myself in the String API. Here is one of the values:

var fileUrl:String = item.split( "install " )[1].slice(1,-1) as String;


This code reads like so:
Split a string into an array where "install " occurs and obtain the 2nd array value. Then slice one past the first character and one before the last to obtain the String url to pass into the "install" command.

Cheers,

Ted :)

7 Responses to “Using String.slice with negative numbers”

  1. # Blogger ifunk

    We should also celebrate the fact AS3 now has great regular expression support.

    var fileUrl:String = item.match(/install .(.+).$/)[1] as String;  

  2. # Blogger Kyle Hayes

    Hmm, Python has this very same feature which is very cool. Good to know AS3s got it.  

  3. # Anonymous Anonymous

    That code might read like that to a few but it's hideous in terms of code readability, maintainability and debug-ability. It's nothing but a developer ego trip.  

  4. # Blogger Ted Patrick

    anonymous,

    Get over it, it works!

    There are 1000's of ways to program some are clearly better than others but if it works it is hard to argue with it.

    Ted :)  

  5. # Blogger mkeefe

    @Ted, your last comment should be law :)

    I actually like how this is accomplished, thanks for sharing.  

  6. # Blogger Richard C Haven

    "Get over it, it works!

    There are 1000's of ways to program some are clearly better than others but if it works it is hard to argue with it."

    I do argue with it if it is hard to read and understand. Anything more than a one-time utility needs to be maintainable, and even the original author will not remember what clever thing they did in a week, let alone a month. Treat yourself as you would have others treat you and use obvious, commented code.

    Cheers  

  7. # Anonymous Anonymous

    Ending the example string literal with "!!!" and then providing examples that deal with the end of the string was a little unclear, "hello from Paris" might have been a better choice, but thanks anyway.

    -sam  

Post a Comment

Where to find me:

Ted on Twitter - @AdobeTed
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe


Latest

Lists

Links

Jobs

Flex Jobs
city, state, zip

Archives