get remaing characters in a string

Discussion in 'Ruby' started by Claus Guttesen, Jan 7, 2007.

  1. Hi.

    I need to get the last three characters in a string. I couldn't find a
    function in the String class so I came up with this:

    class Remainder

    attr_writer :n

    def initialize(t = "", n = 0)
    @t = t.to_s
    @n = n.to_i
    end

    def get
    get_remaining_letters
    end

    private
    def get_remaining_letters
    s = @t.size
    if s < @n
    r = 0
    else
    r = s - @n
    end

    ft = @t.slice(r, @n)
    end

    end


    t = Remainder.new("Guttesen", 5)
    puts t.get
    tesen
    t.n = 4
    puts t.get
    esen

    Did I reinvent the wheel or does it make sense to add it to String?

    regards
    Claus
    Claus Guttesen, Jan 7, 2007
    #1
    1. Advertising

  2. You can use String#[] with a Range instead:
    >> "asdfhsdfabc"[-3..-1]

    => "abc"

    or you can specify the length:
    >> "asdfhsdfabc"[-3, 3]

    => "abc"

    Negative numbers count from the end of the string. So basically the
    strings length plus your negative number is the position from the start
    of the string.

    Dan

    Claus Guttesen wrote:
    > Hi.
    >
    > I need to get the last three characters in a string. I couldn't find a
    > function in the String class so I came up with this:
    >
    > class Remainder
    >
    > attr_writer :n
    >
    > def initialize(t = "", n = 0)
    > @t = t.to_s
    > @n = n.to_i
    > end
    >
    > def get
    > get_remaining_letters
    > end
    >
    > private
    > def get_remaining_letters
    > s = @t.size
    > if s < @n
    > r = 0
    > else
    > r = s - @n
    > end
    >
    > ft = @t.slice(r, @n)
    > end
    >
    > end
    >
    >
    > t = Remainder.new("Guttesen", 5)
    > puts t.get
    > tesen
    > t.n = 4
    > puts t.get
    > esen
    >
    > Did I reinvent the wheel or does it make sense to add it to String?
    >
    > regards
    > Claus
    >
    >
    Daniel Finnie, Jan 7, 2007
    #2
    1. Advertising

  3. Claus Guttesen

    Guest

    On Sun, 7 Jan 2007, Claus Guttesen wrote:

    > Hi.
    >
    > I need to get the last three characters in a string. I couldn't find a
    > function in the String class so I came up with this:
    >
    > class Remainder
    >
    > attr_writer :n
    >
    > def initialize(t = "", n = 0)
    > @t = t.to_s
    > @n = n.to_i
    > end
    >
    > def get
    > get_remaining_letters
    > end
    >
    > private
    > def get_remaining_letters
    > s = @t.size
    > if s < @n
    > r = 0
    > else
    > r = s - @n
    > end
    >
    > ft = @t.slice(r, @n)
    > end
    >
    > end
    >
    >
    > t = Remainder.new("Guttesen", 5)
    > puts t.get
    > tesen
    > t.n = 4
    > puts t.get
    > esen
    >
    > Did I reinvent the wheel or does it make sense to add it to String?


    You can index strings from the right with negative numbers:

    "abcdef"[-3..-1] => "def"

    I think that might be all you need.


    David

    --
    Q. What is THE Ruby book for Rails developers?
    A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
    Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
    A. Ruby Power and Light, LLC (http://www.rubypal.com)
    , Jan 7, 2007
    #3
  4. > > I need to get the last three characters in a string. I couldn't find a
    > > function in the String class so I came up with this:
    > >

    > You can index strings from the right with negative numbers:
    >
    > "abcdef"[-3..-1] => "def"
    >
    > I think that might be all you need.


    I see. Thank you very much for all the replies.

    regards
    Claus
    Claus Guttesen, Jan 7, 2007
    #4
  5. Claus Guttesen wrote:
    > Hi.
    >
    > I need to get the last three characters in a string. I couldn't find a
    > function in the String class so I came up with this:
    >
    > class Remainder
    >
    > attr_writer :n
    >
    > def initialize(t = "", n = 0)
    > @t = t.to_s
    > @n = n.to_i
    > end
    >
    > def get
    > get_remaining_letters
    > end
    >
    > private
    > def get_remaining_letters
    > s = @t.size
    > if s < @n
    > r = 0
    > else
    > r = s - @n
    > end
    >
    > ft = @t.slice(r, @n)
    > end
    >
    > end
    >
    >
    > t = Remainder.new("Guttesen", 5)
    > puts t.get
    > tesen
    > t.n = 4
    > puts t.get
    > esen
    >
    > Did I reinvent the wheel or does it make sense to add it to String?
    >
    > regards
    > Claus


    If the length of the string is less than 3,
    the entire string will be returned.

    s = "ab"
    s[ /.{0,3}$/ ]
    s[-3,3] || s
    William James, Jan 7, 2007
    #5
    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:
    2
    Views:
    751
  2. derrick
    Replies:
    5
    Views:
    761
    derrick
    Dec 17, 2004
  3. akshar108 via DotNetMonster.com

    class object remaing same data

    akshar108 via DotNetMonster.com, Oct 16, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    264
    akshar108 via DotNetMonster.com
    Oct 16, 2007
  4. Ken Fine
    Replies:
    2
    Views:
    182
    Ken Fine
    Feb 5, 2004
  5. Kenneth McDonald
    Replies:
    2
    Views:
    110
    Roberto Casadei
    Sep 24, 2008
Loading...

Share This Page