Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > get remaing characters in a string

Reply
Thread Tools

get remaing characters in a string

 
 
Claus Guttesen
Guest
Posts: n/a
 
      01-07-2007
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

 
Reply With Quote
 
 
 
 
Daniel Finnie
Guest
Posts: n/a
 
      01-07-2007
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
>
>


 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-07-2007
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)

 
Reply With Quote
 
Claus Guttesen
Guest
Posts: n/a
 
      01-07-2007
> > 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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      01-07-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?
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert ascii codes in a string to a string of characters smitty Ruby 4 09-08-2008 11:24 PM
class object remaing same data akshar108 via DotNetMonster.com ASP .Net 0 10-16-2007 05:25 AM
Finding string with "wild" characters in another string Paweł C Programming 9 07-13-2004 03:23 PM
Finding string with "wild" characters in another string Paweł C++ 2 07-09-2004 01:05 PM
Interrogating string for number of characters, response.writing identical number of characters on new line Ken Fine ASP General 2 02-05-2004 03:40 AM



Advertisments