To remove a specific character from a string in Ruby, you can use the `gsub` method.
In this example,
str
with a value of "Hello World"
.gsub
method to replace all occurrences of 'l' with an empty string in str
.gsub
method returns a new string with the specified characters replaced.str
.str = "Hello World"
modified_str = str.gsub('l', '')
puts "Modified string: #{modified_str}"
Modified string: Heo Word
In this example,
str
with a value of "apple"
.gsub
method to replace all occurrences of 'a' with an empty string in str
.gsub
method returns a new string with the specified characters replaced.str
.str = "apple"
modified_str = str.gsub('a', '')
puts "Modified string: #{modified_str}"
Modified string: pple
In this tutorial, we learned How to Remove specific Character from a String in Ruby language with well detailed examples.