Stripping characters off a string

Discussion in 'Ruby' started by Chris Causer, Oct 21, 2008.

  1. Chris Causer

    Chris Causer Guest

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

    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
    Chris Causer, Oct 21, 2008
    #1
    1. Advertising

  2. Chris Causer

    Hugh Sasse Guest

    On Wed, 22 Oct 2008, Chris Causer wrote:

    > 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"
    >

    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
    Hugh Sasse, Oct 21, 2008
    #2
    1. Advertising

  3. Isn't it like
    a = a[0..-5]
    is what you are looking for?

    On Tue, Oct 21, 2008 at 7:28 PM, Chris Causer <> wrote:
    > 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
    >
    Evgeniy Dolzhenko, Oct 21, 2008
    #3
  4. Chris Causer

    Chris Causer Guest

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

    On Tue, Oct 21, 2008 at 4:39 PM, Evgeniy Dolzhenko <>wrote:

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



    Brilliant. Thanks Evgeniy, thanks Hugh.
    Chris Causer, Oct 21, 2008
    #4
  5. Chris Causer

    Marc Heiler Guest

    your_string = "abcdef" # => "abcdef"
    your_string[-4,4] = '' # => ""
    your_string # => "ab"

    --
    Posted via http://www.ruby-forum.com/.
    Marc Heiler, Oct 21, 2008
    #5
  6. Chris Causer

    Chris Causer Guest

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

    On Tue, Oct 21, 2008 at 8:11 PM, Marc Heiler <> 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'.
    Chris Causer, Oct 21, 2008
    #6
  7. Chris Causer wrote:
    > > your_string =3D "abcdef" # =3D> "abcdef"
    > > your_string[-4,4] =3D '' # =3D> ""
    > > your_string =A0 =A0 =A0 =A0 =A0 =A0# =3D> "ab"

    >
    > Does that really work


    Yes, it does. If you copy and paste that code into irb, you'll see the exac=
    t=20
    output you see above (minus the # plus newlines plus the irb prompt)


    > I always thought that=20
    >
    > =A0'string'[a,b]
    >
    > was to take a substring beginning at a of length b.


    That's right.


    > In your case, you'd=20
    > just get the last 4 characters 'cdef'.


    Leaving the characters "ab", which is exactly what he showed in the code=20
    above.

    =2D-=20
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Oct 21, 2008
    #7
  8. Chris Causer

    Chris Causer Guest

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

    On Tue, Oct 21, 2008 at 11:22 PM, Sebastian Hungerecker <
    > wrote:

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


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

    Thanks Marc and Sebastian, but the range method is particularly alluring :)
    Chris Causer, Oct 21, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?THU=?=
    Replies:
    4
    Views:
    1,446
    Joerg Jooss
    Sep 2, 2005
  2. VJ

    Stripping characters...

    VJ, Jan 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    442
    John Timney \( MVP \)
    Jan 17, 2006
  3. et
    Replies:
    3
    Views:
    721
  4. Robert
    Replies:
    1
    Views:
    294
    Robert
    Nov 4, 2005
  5. Paulers
    Replies:
    7
    Views:
    1,911
    Roedy Green
    Jun 29, 2008
Loading...

Share This Page