In Ruby, we can permanently delete characters from a string by using the string.delete
method. It returns a new string with the specified characters removed.
str.delete! other_string
str
: this is the string from which we want to delete some characters permanently.
other_string
: this is a mandatory string that contains the specified characters we want to delete from str
.
The value returned is a string that does not contain the characters specified to remove. If str
is not modified by this method, nothing or nil is returned.
# create some stringsstr1 = "Edpresso"str2 = "is"str3 = "awesome"# delete some charactersstr1.delete! "es"str2.delete! "s"str3.delete! "ew"# print modified stringsputs str1puts str2puts str3
str1
, str2
, str3
.delete
method and pass strings that contain the characters we want to delete.