Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts

    Substring and array

    Is there any overwhelming reason to read a string into an array as opposed to just using substring to grab values from it?

    I have filenames with a specific scheme that has date, topic, etc. embedded in it (i.e. 06122003_request_blahblah.doc) It is a very fixed format, so substring(0,8) will always be the date field and so on.

    Since I am going to parse this, should I just use substring to grab everything I need or use substring to read it into an array and then play around with the array?

    I guess performance is what I'm concerned about. Not so much ease of programming (that's trivial).

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm - depends on what you're doing to do with this when once you've parsed it.

    If all the key parts of the filename are separated by an underscore, then you can do this

    var filename = "06122003_request_blahblah.doc";
    var file = filename.split( /[_.]/g );

    file is now an array that looks like this

    ['06122003','request','blahblah','doc']
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    That's a much easier/compact way than the way I did it. I used functions like getDate(filename) and substring to grab everything. I'm going to modify my code using your idea and switch over to a two dimensional array to hold everything.

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •