Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace substring with another substring (by index from..to)

Tags:

string

ruby

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'
like image 409
freemanoid Avatar asked Nov 01 '13 17:11

freemanoid


1 Answers

str = 'show me the money'
first = 4
last = 11
replacement = '...'
str[first..last] = replacement
str 
#=> 'show...money'
like image 149
freemanoid Avatar answered Nov 11 '22 23:11

freemanoid