From: Josh Cheek Date: 2010-09-22T12:24:17+09:00 Subject: Re: comparing the first half of a string to the second half --001485f90f520d40600490d0ae89 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Sep 21, 2010 at 3:41 PM, joe chesak wrote: > I want to be able to test a string, to see if the second half of the string > is a duplicate of the first part of the string, and if so I just want the > first half (or the second for that matter). I am using ruby-1.9.2. > > I can accomplish this with the following: > > str = 'Cyano The CatCyano The Cat' > > str = str[0..str.size/2-1] if str[0..str.size/2-1] == str[str.size/2..-1] > => "Cyano The Cat" > > Is there a better way? > > Joe > Instead of subtracting 1 from the right side of the range, you can just use 3 dots. str = 'Cyano The CatCyano The Cat' half = str.size / 2 first = str[0...half] last = str[half..-1] str = first if first == last p str --001485f90f520d40600490d0ae89--