7

How I can replace substring from some character to another by other string?

first = 4
last = 11
replacement = '...'
'show me the money'.replace_part(first, last, replacement)
# => 'show...money'
3
  • Could there be a better version of title? In its current form it's not very findable, I'm afraid. But I don't have any alternatives. :/ Commented Nov 1, 2013 at 17:39
  • I know but I also can't come up with better :( Commented Nov 1, 2013 at 17:42
  • 2
    Maybe: "How to replace a substring with another substring" Commented Nov 1, 2013 at 18:00

1 Answer 1

20
str = 'show me the money'
first = 4
last = 11
replacement = '...'
str[first..last] = replacement
str 
#=> 'show...money'
Sign up to request clarification or add additional context in comments.

Comments

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.