comparing the first half of a string to the second half

J

joe chesak

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

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
 
A

Adam Prescott

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

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

Xavier Noria

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

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

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

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

Rob Biedenharn

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
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
S

Steel Steel

joe said:
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
 
S

Steel Steel

joe said:
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
 
J

Josh Cheek

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

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
 
R

(r.*n){2}

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





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?

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top