Reputation: 105
The title isn't that clear, but I wasn't sure how to phrase it. In Ruby,
a = "somestring"
a[1] #Becomes 'o'
a[1..4] #Becomes 'omes'
This selects a desired portion from a string. I was wondering how I should make it so that it selects parts of a string from a fixed starting number to the end of the string, no matter how long it is.
So:
a[3 All the way to the end] should become 'estring'
I could do a[1..10000]
but that feels extremely redundant.
Upvotes: 1
Views: 156
Reputation: 4226
You can use -1
to specify the end of your range - this would point to the last character of a string, no matter the length:
a = "This is a string"
a[2..-1]
#=> "is is a string"
This same approach can be applied to get the second to last character -2
, third to last character -3
, etc.
Hope it helps!
Upvotes: 2