From: James Edward Gray II Date: 2007-07-24T13:10:53+09:00 Subject: Re: finding positions in a string On Jul 23, 2007, at 3:54 PM, bwv549 wrote: > I have a long string and I need to know the starting position of all > substrings matching a particular sequence. Most importantly, it needs > to be fast. Secondly, it would be nice if it was somewhat concise. > > Here's the method: > > def substring_positions(substring, string) > ## fast, concise method?? > end > > my_substring = 'this' > my_string = 'this string this is a string this is it' > > substring_positions(my_substring, my_string) # -> should return > [0, 12, 29] Another idea is to use String#index() with the optional second minimum index parameter which you keep incrementing. James Edward Gray II