0

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?

4 Answers 4

6

Use test[-30..-1] Doc: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-5B-5D

Sign up to request clarification or add additional context in comments.

Comments

1

Use negative indices:

a = "qwertyuiop"
a[-3..-1]
"iop"

Comments

1

Use test[30..-1], which makes more sense to me than test[-30,-1]

Comments

1

If you have activesupport/rails, you can also pass the number of characters to last.

text.last(30)

1 Comment

This solution requires activesupport (rails). Thus the downvotes I guess.

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.