开发者

Ruby: is there a simple one liner for replacing part of a string between two indexes with another string?

开发者 https://www.devze.com 2023-04-12 04:10 出处:网络
Lets say I have: a = \"Stack Overflow\" Is there a function where I can do something like: > a.replace!(3, 10, \" hello \")

Lets say I have:

a = "Stack Overflow"

Is there a function where I can do something like:

> a.replace!(3, 10, " hello ")
> a
=> "Sta hello flow"

or so开发者_开发百科mething similar?

basically, said function I don't know of would remove all text inbetween the specified indexes, then insert the given text after the start index.


Try this:

a = "Stack Overflow"
a[3..9] = " hello "
a


Untested code:

a[3,10] = "hello" ?
0

精彩评论

暂无评论...
验证码 换一张
取 消