Getting the last N bytes of a string:

H

Harry Kakueki

Sorry to come so late in the thread.

This seems to work fine for me.

irb(main):001:0> str = "now is the time for all good men to come"
=> "now is the time for all good men to come"
irb(main):002:0> str[-10..-1]
=> "en to come"

irb(main):001:0> str = "now is the"
=> "now is the"
irb(main):002:0> str[-10..-1]
=> "now is the"

So I really see no need for a special method to obtain the last x
bytes of a string.

The problem is when there are not 10 bytes in the string.

str = "abc"
p str[-10..-1] # nil

Harry
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top