Ruby | String insert Method
insert is a String class method in Ruby which is used to inserts the specified string before the character at the given index, modifying the given one. Negative indices count from the end of the string, and insert after the given character.
Syntax: str.insert(index, other_str)
Parameters: Here, str is the given string.
Returns: A modified string.
Example 1:
# Ruby program to demonstrate # the insert method # Taking a string and # using the method puts "Ruby".insert(0, 'Z') puts "Program".insert(3, 'Z') |
chevron_right
filter_none
Output:
ZRuby ProZgram
Example 2:
# Ruby program to demonstrate # the insert method # Taking a string and # using the method puts "Sample".insert(-4, 'Z') puts "Articles".insert(-5, 'Z') |
chevron_right
filter_none
Output:
SamZple ArtiZcles
Recommended Posts:
- Ruby | String eql? Method
- Ruby | String * Method
- Ruby | String << Method
- Ruby | String + Method
- Ruby | String <=> Method
- Ruby | String hex Method
- Ruby | String == method
- Ruby | String chr Method
- Ruby | String === Method
- Ruby | String =~ Method
- Ruby | Array class insert() function
- Ruby | String delete_suffix! Method
- Ruby | String each_byte Method
- Ruby | String delete_prefix Method
- Ruby | String capitalize! Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



