On Sep 21, 2010, at 4: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


irb> str = 'Cyano The CatCyano The Cat'
=> "Cyano The CatCyano The Cat"
irb> str =~ /\A(.*)\1\z/
=> 0
irb> $1
=> "Cyano The Cat"

irb> str = "I am not a twinNeither am I"
=> "I am not a twinNeither am I"
irb> str =~ /\A(.*)\1\z/
=> nil

-Rob

Rob Biedenharn		
Rob / AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab / GaslightSoftware.com		http://GaslightSoftware.com/