...

View Full Version : Substring and array



arnyinc
06-13-2003, 03:26 PM
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).

beetle
06-13-2003, 04:02 PM
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']

arnyinc
06-13-2003, 05:11 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum