From: Timothy Hunter Date: 2007-03-28T09:13:23+09:00 Subject: Re: String Intersection. A better way Daniel N wrote: > Hi all, > > I have an issue with strings. I want the intesection of two strings > but I'm > not sure how to go about it. I have two cases. > > Case 1: > String 1 has the tail end intersecting with the start of string 2.. eg: > > String 1: The quick brown fox jumped over the lazy dog. > String 2: fox jumped over the lazy dog. And in other news bla bla bla > > The problem is I don't know how much is intersecting. > What I've done is to take string 1 and say psudo code > > until match found > string2 =~ /^(value of string1)/ > if match found set flag > else delete first char of string1 > loop again > > What is a better way to do this? > > Case 2: > > The start of String1 intersects with the end of string2 eg: > > String1: And here we are with another string. > String2: bla bla bla. And here we are > > I don't really know about this one yet... I haven't gotten to it. > > Can anyone pls suggest what I might do? > > Thanx > Daniel > Take a look at the MatchData class, particularly the #begin and #offset methods.