使用Ruby,如何替换字符串中的一段字符?例如,给出字符串:
我该怎样做这件事?
hellothere
如果我想将索引位置为2到5(含)的字符替换为“#”,以得到一个字符串
he####here
我该怎样做这件事?
hellothere
如果我想将索引位置为2到5(含)的字符替换为“#”,以得到一个字符串
he####here
def replace_in_string(str, replace, start, finish)
str[start..finish] = replace * (finish + 1 - start)
str
end
p replace_in_string 'hellothere', '#', 2, 5
# "he####here"