Comparing the first half of a string to the second half

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

I think a regular expression is probably the most succinct, cleanest
approach for this:

s = "Cyano The Cat" * 2
if s =~ /^(.+)\1$/
  puts $1
end

···

On Tue, Sep 21, 2010 at 9:41 PM, joe chesak <rubytalk@chesak.com> 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

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

str = 'Cyano The CatCyano The Cat'

=> "Cyano The CatCyano The Cat"

str =~ /\A(.*)\1\z/

=> 0

$1

=> "Cyano The Cat"

str = "I am not a twinNeither am I"

=> "I am not a twinNeither am I"

str =~ /\A(.*)\1\z/

=> nil

-Rob

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

···

On Sep 21, 2010, at 4:41 PM, joe chesak wrote:

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

str = 'Cyano The CatCyano The Cat'
o,t=a.each_slice(a.size/2).map{|x|x}
str = o if t==o

···

--
Posted via http://www.ruby-forum.com/\.

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

str = 'Cyano The CatCyano The Cat'
o,t=str.chars.to_a.each_slice(str.size/2).map{|x|x}
str = o if t==o

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Tue, Sep 21, 2010 at 3:41 PM, joe chesak <rubytalk@chesak.com> 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

Clever, but almost: "aa\n" passes and it shouldn't.

For an arbitrary string you want \A and \z (and /m).

···

On Tue, Sep 21, 2010 at 10:55 PM, Adam Prescott <mentionuse@gmail.com> wrote:

I think a regular expression is probably the most succinct, cleanest
approach for this:

s = "Cyano The Cat" * 2
if s =~ /^(.+)\1$/
puts $1
end

x = s.size
y = x / 2
r = x % 2
puts "#{s[0..y-1]}" if ( r == 0) && (s[0..y-1] == s[y..-1])

···

On Sep 21, 11:24 pm, Josh Cheek <josh.ch...@gmail.com> wrote:

[Note: parts of this message were removed to make it a legal post.]

On Tue, Sep 21, 2010 at 3:41 PM, joe chesak <rubyt...@chesak.com> 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