How to use string.delete method in Ruby
In this tutorial, we are going to learn about how to use string.delete in Ruby with the help of examples.
The string.delete method in ruby helps us to delete the specified characters from a string and returns the new string without deleted characters.
Using string.delete method
string.delete "character_list"
The string.delete method accepts one argument which is character_list where we need to specify what characters we need to remove from a given string.
Here is an example:
str = "hamburger"
# it deletes the characters ham from the given string
result = str.delete "ham"
puts result
Output:
"burger"
In the example above, we have specified “ham” as a argument list to the string.delete method. So, it deletes the characters “h, a, m” from the string and returns a new string without those deleted characters.
More examples:
str1 = "audi"
# it deletes the characters d,i from the given string
result1 = str1.delete "di"
str2 = "benz"
# it deletes the characters e,n from the given string
result2 = str2.delete "en"
str3 = "honda"
# it deletes the characters a from the given string
result3 = str3.delete "a"