mirror of
https://github.com/ruby/ruby.git
synced 2026-01-26 12:14:51 +00:00
16 lines
552 B
Plaintext
16 lines
552 B
Plaintext
Inserts the given +other_string+ into +self+; returns +self+.
|
|
|
|
If the given +index+ is non-negative, inserts +other_string+ at offset +index+:
|
|
|
|
'foo'.insert(0, 'bar') # => "barfoo"
|
|
'foo'.insert(1, 'bar') # => "fbaroo"
|
|
'foo'.insert(3, 'bar') # => "foobar"
|
|
'こんにちは'.insert(2, 'bar') # => "こんbarにちは"
|
|
|
|
If the +index+ is negative, counts backward from the end of +self+
|
|
and inserts +other_string+ _after_ the offset:
|
|
|
|
'foo'.insert(-2, 'bar') # => "fobaro"
|
|
|
|
Related: see {Modifying}[rdoc-ref:String@Modifying].
|