Getting the last four digits of a string or number

I want to trim down a phone number to the last four digits before saving
it to the db. How would I do this? For example, 800-555-1212 which
could also be 8005551212, i want to save just 1212 (last four digits)
only.

phone_number = “number here”
last_four_digits = phone_number[phone_number.length - 4 ,4]

-=-

Neeraj K. wrote:

phone_number = “number here”
last_four_digits = phone_number[phone_number.length - 4 ,4]

-=-

Thanks Neeraj!

num_str = ‘1234567890’
last_four = num_str[-4…-1]

Should also work

Ciao!

Gustav P.
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs