如何用另一个子串替换子串(通过索引from..to)

时间:2013-11-01 17:36:19

标签: ruby string

如何通过其他字符串将子字符串从某个字符替换为另一个字符?

first = 4
last = 11
replacement = '...'
'show me the money'.replace_part(first, last, replacement)
# => 'show...money'

1 个答案:

答案 0 :(得分:14)

str = 'show me the money'
first = 4
last = 11
replacement = '...'
str[first..last] = replacement
str 
#=> 'show...money'