To replace a character at a specific index in a string in Ruby, you can convert the string to an array of characters, modify the character at the desired index, and then join the characters back into a string.
In this example,
str
.chars()
.index
with the character newChar
.join('')
.str
.str = "Hello World"
index = 6
new_char = 'x'
char_array = str.chars
char_array[index] = new_char
str = char_array.join('')
puts str
Hello xorld
In this tutorial, we learned How to Replace Character at Specific Index in String in Ruby language with well detailed examples.