How do you get the tail end of a string?

Discussion in 'Ruby' started by Just Another Victim of the Ambient Morality, Oct 30, 2009.

  1. I'm actually hoping this is an embarrassing question but how do you get
    the tail end of a string? All I've figured out is this:

    index = 4
    string[index, string.size - index]

    ...but surely there's a better way. This happens to me a lot and I
    really appreciate Python's curt syntax in this case: string[index:]
    What's the Ruby way?
    Thank you!
    Just Another Victim of the Ambient Morality, Oct 30, 2009
    #1
    1. Advertising

  2. Just Another Victim of the Ambient Morality wrote:
    > I'm actually hoping this is an embarrassing question but how do you get
    > the tail end of a string? All I've figured out is this:
    >
    > index = 4
    > string[index, string.size - index]
    >
    > ...but surely there's a better way. This happens to me a lot and I
    > really appreciate Python's curt syntax in this case: string[index:]
    > What's the Ruby way?
    > Thank you!
    >
    >

    You mean like string[-1]?
    Michael W. Ryder, Oct 30, 2009
    #2
    1. Advertising

  3. Michael W. Ryder wrote:
    > Just Another Victim of the Ambient Morality wrote:
    >> I'm actually hoping this is an embarrassing question but how do
    >> you get the tail end of a string? All I've figured out is this:
    >>
    >> index = 4
    >> string[index, string.size - index]
    >>
    >> ...but surely there's a better way. This happens to me a lot and
    >> I really appreciate Python's curt syntax in this case: string[index:]
    >> What's the Ruby way?
    >> Thank you!
    >>

    > You mean like string[-1]?


    I'm sorry, I missed the part about the index number of characters. Try
    string[-index, index].
    Michael W. Ryder, Oct 30, 2009
    #3
  4. Just Another Victim of the Ambient Morality

    7stud -- Guest

    > I'm actually hoping this is an embarrassing question but how do you get
    > the tail end of a string? All I've figured out is this:
    >
    > index = 4
    > string[index, string.size - index]
    >
    > ...but surely there's a better way. This happens to me a lot and I
    > really appreciate Python's curt syntax in this case: string[index:]
    > What's the Ruby way?
    >
    > Thank you!


    str = "hello"
    result = str[1..-1]

    p result
    --output:--
    "ello"
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 30, 2009
    #4
  5. Just Another Victim of the Ambient Morality

    7stud -- Guest

    7stud -- wrote:
    >> I'm actually hoping this is an embarrassing question but how do you get
    >> the tail end of a string? All I've figured out is this:
    >>
    >> index = 4
    >> string[index, string.size - index]
    >>
    >> ...but surely there's a better way. This happens to me a lot and I
    >> really appreciate Python's curt syntax in this case: string[index:]
    >> What's the Ruby way?
    >>
    >> Thank you!

    >
    > str = "hello"
    > result = str[1..-1]
    >
    > p result
    > --output:--
    > "ello"


    ...which is very similar to python--except python doesn't include the
    last index position in the slice:

    str = "hello"
    result = str[1:-1]
    print result

    --output:--
    ell
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 30, 2009
    #5
  6. Just Another Victim of the Ambient Morality wrote:
    > I'm actually hoping this is an embarrassing question but how do you get
    > the tail end of a string? All I've figured out is this:
    >

    using reg-ex

    irb(main):102:0> "quick brown fox".scan /.$/
    => ["x"]

    irb(main):103:0> "yo mama".scan /.$/
    => ["a"]


    See Reg-ex section here: http://www.ruby-doc.org/docs/ProgrammingRuby/


    You can convert the string into an array, and use Array.last and Array.pop
    methods to consume from the end

    irb(main):090:0> letters = "quick brown fox".scan /./
    => ["q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x"]

    irb(main):091:0> letters.last
    => "x"
    irb(main):092:0> letters.pop
    => "x"

    irb(main):093:0> letters.last
    => "o"
    irb(main):094:0> letters.pop
    => "o"

    irb(main):095:0> letters.last
    => "f"
    irb(main):096:0> letters.pop
    => "f"

    --
    Kind Regards,
    Rajinder Yadav

    http://DevMentor.org

    Do Good! - Share Freely, Enrich and Empower people to Transform their lives.
    Rajinder Yadav, Oct 30, 2009
    #6
  7. Hi,

    Am Freitag, 30. Okt 2009, 13:35:24 +0900 schrieb Rajinder Yadav:
    > Just Another Victim of the Ambient Morality wrote:
    >> I'm actually hoping this is an embarrassing question but how do you
    >> get the tail end of a string? All I've figured out is this:

    > using reg-ex
    >
    > irb(main):102:0> "quick brown fox".scan /.$/
    > => ["x"]


    Why scan? There's just one match.

    "quick brown fox"[ /.\z/]

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #7
  8. Hi,

    Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
    > Michael W. Ryder wrote:
    >> Just Another Victim of the Ambient Morality wrote:
    >>> I'm actually hoping this is an embarrassing question but how do you
    >>> get the tail end of a string? All I've figured out is this:
    >>>
    >>> index = 4
    >>> string[index, string.size - index]
    >>>
    >>> ...but surely there's a better way.
    >>>

    >> You mean like string[-1]?

    >
    > I'm sorry, I missed the part about the index number of characters. Try
    > string[-index, index].


    That's really ugly. You shouldn't have to mention `index' twice.
    I would even support a language extension to solve this problem.

    For my private use I wrote a suite of methods

    String#head
    String#tail
    String#starts_with
    String#ends_with

    Probably I should make an own gem of it. I'll think about that
    this weekend. For so long have a look at my personal library:

    http://bertram-scharpf.homelinux.com:8808/doc_root/bs-ruby-2.7/rdoc/classes/String.html

    By the way: Still I'm convinced that there should be a
    `String#notempty?' method corresponding to `Numeric#nonzero?'.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #8
  9. Bertram Scharpf wrote:
    > For my private use I wrote a suite of methods

    [snip]
    > String#starts_with
    > String#ends_with


    What's wrong with the built-in #start_with and #end_with, except the
    incorrect grammar?
    --
    Posted via http://www.ruby-forum.com/.
    Albert Schlef, Oct 30, 2009
    #9
  10. [Note: parts of this message were removed to make it a legal post.]

    2009/10/30 Just Another Victim of the Ambient Morality <
    >

    > I'm actually hoping this is an embarrassing question but how do you get
    > the tail end of a string? All I've figured out is this:
    >
    > index = 4
    > string[index, string.size - index]
    >


    is equivalent to

    string[index..-1]

    -Thomas


    --
    Thomas Preymesser

    http://thopre.googlepages.com/
    http://thopre.wordpress.com/

    Pablo Picasso<http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html>
    - "Computers are useless. They can only give you answers."
    Thomas Preymesser, Oct 30, 2009
    #10
  11. Hi,

    Am Freitag, 30. Okt 2009, 20:53:02 +0900 schrieb Albert Schlef:
    > Bertram Scharpf wrote:
    > > For my private use I wrote a suite of methods

    > [snip]
    > > String#starts_with
    > > String#ends_with

    >
    > What's wrong with the built-in #start_with and #end_with, except the
    > incorrect grammar?


    That's Ruby 1.8.7/1.9; I wrote it years ago.

    It is satisfying me and it is giving me hope that parts of my
    proposal are already realized.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #11
  12. Hi --

    On Fri, 30 Oct 2009, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
    >> Michael W. Ryder wrote:
    >>> Just Another Victim of the Ambient Morality wrote:
    >>>> I'm actually hoping this is an embarrassing question but how do you
    >>>> get the tail end of a string? All I've figured out is this:
    >>>>
    >>>> index = 4
    >>>> string[index, string.size - index]
    >>>>
    >>>> ...but surely there's a better way.
    >>>>
    >>> You mean like string[-1]?

    >>
    >> I'm sorry, I missed the part about the index number of characters. Try
    >> string[-index, index].

    >
    > That's really ugly. You shouldn't have to mention `index' twice.


    I don't think there's anything wrong with it. It works well, and
    there's nothing stylistically wrong with using a local variable twice.
    If index is a method that does a (re)calculation every time, you'd
    want to cache it, but that's not the case in the example.


    David

    --
    The Ruby training with D. Black, G. Brown, J.McAnally
    Compleat Jan 22-23, 2010, Tampa, FL
    Rubyist http://www.thecompleatrubyist.com

    David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
    David A. Black, Oct 30, 2009
    #12
  13. Bertram Scharpf wrote:
    >
    > By the way: Still I'm convinced that there should be a
    > `String#notempty?' method corresponding to `Numeric#nonzero?'.
    >


    class String
    def notempty?
    !self.empty?
    end
    end

    Good enough?
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Oct 30, 2009
    #13
  14. Hi,

    Am Freitag, 30. Okt 2009, 22:32:19 +0900 schrieb Aldric Giacomoni:
    > Bertram Scharpf wrote:
    > >
    > > By the way: Still I'm convinced that there should be a
    > > `String#notempty?' method corresponding to `Numeric#nonzero?'.
    > >

    >
    > class String
    > def notempty?
    > !self.empty?
    > end
    > end


    My question was not _how_ to implement it but _why_ _not_ to add
    it to the interpreter.

    Your method is returning `true' what Numeric#nonzero? will never
    do.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #14
  15. Hi,

    Am Freitag, 30. Okt 2009, 21:06:11 +0900 schrieb David A. Black:
    > On Fri, 30 Oct 2009, Bertram Scharpf wrote:
    >>>
    >>> string[-index, index].

    >>
    >> That's really ugly. You shouldn't have to mention `index' twice.

    >
    > I don't think there's anything wrong with it. It works well, and
    > there's nothing stylistically wrong with using a local variable twice.


    What when it's a method call?

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #15
  16. Bertram Scharpf wrote:
    > My question was not _how_ to implement it but _why_ _not_ to add
    > it to the interpreter.
    >
    > Your method is returning `true' what Numeric#nonzero? will never
    > do.
    >
    > Bertram


    You want a method which returns "nil" if the string is empty, and the
    string itself if the string is nonempty?

    class String
    def nonempty?
    self.empty? ? nil : self
    end
    end

    Happy? ;-)
    Why wouldn't this be implemented by default, er, I don't know, I really
    can't think of a time when I'd need this type of behavior.
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Oct 30, 2009
    #16
  17. Hi --

    On Fri, 30 Oct 2009, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Freitag, 30. Okt 2009, 21:06:11 +0900 schrieb David A. Black:
    >> On Fri, 30 Oct 2009, Bertram Scharpf wrote:
    >>>>
    >>>> string[-index, index].
    >>>
    >>> That's really ugly. You shouldn't have to mention `index' twice.

    >>
    >> I don't think there's anything wrong with it. It works well, and
    >> there's nothing stylistically wrong with using a local variable twice.

    >
    > What when it's a method call?


    Well, that would be the case I addressed in the part of my paragraph
    you deleted :)

    >> If index is a method that does a (re)calculation every time, you'd
    >> want to cache it, but that's not the case in the example.



    David

    --
    The Ruby training with D. Black, G. Brown, J.McAnally
    Compleat Jan 22-23, 2010, Tampa, FL
    Rubyist http://www.thecompleatrubyist.com

    David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
    David A. Black, Oct 30, 2009
    #17
  18. Bertram Scharpf wrote:
    > Hi,
    >
    > Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
    >>> You mean like string[-1]?

    >>
    >> I'm sorry, I missed the part about the index number of characters. Try
    >> string[-index, index].

    >
    > That's really ugly. You shouldn't have to mention `index' twice.


    And you don't. string[-index..-1] does the trick.

    > I would even support a language extension to solve this problem.


    No need.

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Oct 30, 2009
    #18
  19. Hi,

    Am Samstag, 31. Okt 2009, 00:12:01 +0900 schrieb Marnen Laibow-Koser:
    > Bertram Scharpf wrote:
    > > Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
    > >> string[-index, index].

    > >
    > > That's really ugly. You shouldn't have to mention `index' twice.

    >
    > And you don't. string[-index..-1] does the trick.
    >
    > > I would even support a language extension to solve this problem.

    >
    > No need.


    You seem to be quite sure what you need. Look at this:

    irb(main):001:0> def timer ; start = Time.now ; yield ; Time.now - start ; end
    => nil
    irb(main):002:0> index = 2
    => 2
    irb(main):003:0> timer do 100_000.times { "hello"[-index..-1] } end
    => 0.888826
    irb(main):004:0> timer do 100_000.times { "hello".tail index } end
    => 0.21357

    Your calculation needs more than four times as much processor
    time. Don't you need to save time?

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #19
  20. Hi,

    Am Freitag, 30. Okt 2009, 22:45:10 +0900 schrieb Aldric Giacomoni:
    > Bertram Scharpf wrote:
    > > My question was not _how_ to implement it but _why_ _not_ to add
    > > it to the interpreter.

    >
    > [yet another implementation]
    >
    > Why wouldn't this be implemented by default, er, I don't know, I really
    > can't think of a time when I'd need this type of behavior.


    That's just about _you_ but not about other Ruby programmers.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Oct 30, 2009
    #20
    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. Replies:
    4
    Views:
    2,242
    Chris Uppal
    Feb 16, 2006
  2. Bengt Richter

    raw string tail escape revisited

    Bengt Richter, Aug 9, 2003, in forum: Python
    Replies:
    2
    Views:
    417
    Bengt Richter
    Aug 9, 2003
  3. Sumika
    Replies:
    4
    Views:
    601
    Emmanuel Delahaye
    Jul 15, 2005
  4. Terry Michaels

    Tail Call Optimization (Tail Recursion)

    Terry Michaels, Apr 18, 2011, in forum: Ruby
    Replies:
    16
    Views:
    309
    Robert Klemme
    Apr 20, 2011
  5. Replies:
    6
    Views:
    136
Loading...

Share This Page