From: andrew_queisser@... (andrew queisser) Date: 2002-02-02T07:55:12+09:00 Subject: Re: Newbie question Niklas Frykholm wrote in message news:... > > What's the best way to return a substring that consists of the first 4 > > words, if words are comma-delimited, e.g.: > > > > My first choice was: > > > > line.split(",")[0..3].join(",") > > > > but that can't be very efficient. I tried to do an explicit loop but I > > couldn't get it any better than this: > > On the other hand the solution above is very readable. Are you sure that this > is where you want to save your cycles? > > A solution which is similar to the one above but is more effective when the > string contains many words is: > > line.split(",", 5)[0..3].join(",") > > // Niklas Ahh, yes, thank you! That's what I was looking for. I missed the 'limit' parameter in the documentation for the split function when I read about it. I liked the split-join thing but the files I'm processing are huge so the speed difference between the split-join and the explicit loop was large. Thanks to the others for the fancy regexp's. It'll take me a while to digest those. Andrew