# Stripping characters off a string

**URL:** <https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450>\
**Category:** Ruby\
**Created:** [October 21, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450 "2008-10-21T17:31:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 21, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/1 "2008-10-21T17:31:07Z")

</div>

Hi everyone,

I know this is an easy question, but I want to know the best way to do  
this  
(i.e. the most Rubyesque).

How do I strip the last four characters off a string of undetermined  
length?  
I’m sure it is a one liner and doesn’t require regexp. I currently have:

irb(main):010:0\> a=‘80/tcp’  
=\> “80/tcp”  
irb(main):011:0\> a[0,a.length-4]  
=\> “80”

irb(main):010:0\> b=‘5666/tcp’  
=\> “5666/tcp”  
irb(main):011:0\> b[0,a.length-4]  
=\> “5666”

Which is two lines, but I’m hoping you smart guys can help me out 😉 I’m  
guessing “chop” would work, but I’m not sure if that’s the most elegant  
solution

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 21, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/2 "2008-10-21T17:35:14Z")

</div>

On Wed, 22 Oct 2008, Chris Causer wrote:

> =\> “80/tcp”  
> irb(main):011:0\> a[0,a.length-4]  
> =\> “80”

How does:

irb(main):001:0\> str = “80/tcp”  
=\> “80/tcp”  
irb(main):002:0\> str[0…-4]  
=\> “80”  
irb(main):003:0\>

look?  
Hugh

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 21, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/3 "2008-10-21T17:41:45Z")

</div>

Isn’t it like  
a = a[0…-5]  
is what you are looking for?

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 21, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/4 "2008-10-21T21:13:10Z")

</div>

your\_string = “abcdef” # =\> “abcdef”  
your\_string[-4,4] = ‘’ # =\> “”  
your\_string # =\> “ab”

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 22, 2008, 12:20am UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/5 "2008-10-22T00:20:26Z")

</div>

On Tue, Oct 21, 2008 at 8:11 PM, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> your\_string = “abcdef” # =\> “abcdef”  
> your\_string[-4,4] = ‘’ # =\> “”  
> your\_string # =\> “ab”

Does that really work Marc? I always thought that

‘string’[a,b]

was to take a substring beginning at a of length b. In your case, you’d  
just  
get the last 4 characters ‘cdef’.

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 22, 2008, 12:26am UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/6 "2008-10-22T00:26:53Z")

</div>

Chris Causer wrote:

> > your\_string = “abcdef” # =\> “abcdef”  
> > your\_string[-4,4] = ‘’ # =\> “”  
> > your\_string # =\> “ab”
> 
> Does that really work

Yes, it does. If you copy and paste that code into irb, you’ll see the  
exact  
output you see above (minus the # plus newlines plus the irb prompt)

> I always thought that
> 
> ‘string’[a,b]
> 
> was to take a substring beginning at a of length b.

That’s right.

> In your case, you’d  
> just get the last 4 characters ‘cdef’.

Leaving the characters “ab”, which is exactly what he showed in the code  
above.

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 21, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/7 "2008-10-21T18:47:31Z")

</div>

On Tue, Oct 21, 2008 at 4:39 PM, Evgeniy D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Isn’t it like  
> a = a[0…-5]  
> is what you are looking for?

Brilliant. Thanks Evgeniy, thanks Hugh.

---

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [October 22, 2008, 12:40am UTC](https://www.ruby-forum.com/t/stripping-characters-off-a-string/151450/8 "2008-10-22T00:40:21Z")

</div>

On Tue, Oct 21, 2008 at 11:22 PM, Sebastian H. \<  
removed\_email\_address@domain.invalid\> wrote:

> 

Ah, now I see. I missed the =’’ on the second line.

Thanks Marc and Sebastian, but the range method is particularly alluring  
🙂
