ruby/doc/string/insert.rdoc
2025-08-21 18:52:15 -04:00

17 lines
632 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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ст" # Characters, not bytes.
'こんにちは'.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].