# Comparing the first half of a string to the second half

**URL:** <https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339>\
**Category:** Ruby\
**Created:** [September 21, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339 "2010-09-21T22:47:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 21, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/1 "2010-09-21T22:47:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 21, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/2 "2010-09-21T22:55:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 21, 2010, 11:02pm UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/3 "2010-09-21T23:02:12Z")

</div>

On Tue, Sep 21, 2010 at 10:55 PM, Adam P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

Clever, but almost: “aa\n” passes and it shouldn’t.

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

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 21, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/4 "2010-09-21T23:16:50Z")

</div>

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

> 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 B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 22, 2010, 5:11am UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/5 "2010-09-22T05:11:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 22, 2010, 5:09am UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/6 "2010-09-22T05:09:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 22, 2010, 5:24am UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/7 "2010-09-22T05:24:31Z")

</div>

On Tue, Sep 21, 2010 at 3:41 PM, joe chesak [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![joe\_chesak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@joe\_chesak](https://www.ruby-forum.com/u/joe_chesak)\
**Post date:** [September 23, 2010, 5:40am UTC](https://www.ruby-forum.com/t/comparing-the-first-half-of-a-string-to-the-second-half/195339/8 "2010-09-23T05:40:27Z")

</div>

On Sep 21, 11:24 pm, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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])
