Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a string in the variable text. If I do text[0 .. 29] I get the first 30 characters. What if I want the last part of string which I don't know the length?
text
text[0 .. 29]
Use test[-30..-1] Doc: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-5B-5D
test[-30..-1]
Add a comment
Use negative indices:
a = "qwertyuiop" a[-3..-1] "iop"
Use test[30..-1], which makes more sense to me than test[-30,-1]
test[30..-1]
test[-30,-1]
If you have activesupport/rails, you can also pass the number of characters to last.
last
text.last(30)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.