Skip to content
Related Articles

Related Articles

Ruby | String insert Method
  • Last Updated : 09 Dec, 2019
GeeksforGeeks - Summer Carnival Banner

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')   

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')   

Output:

SamZple
ArtiZcles

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :