w3resource

Ruby String Exercises: Remove a substring from a specified string

Ruby String: Exercise-8 with Solution

Write a Ruby program to remove a substring from a specified string.

Ruby String Exercises: Remove a substring from a specified string

Ruby Code:

def check_string(my_string, substr)
  my_string.slice! substr
  return my_string
end
print check_string("JavaScript", "Script")
print "\n",check_string("HTMLCSS", "HTML")

Output:

Java
CSS

Flowchart:

Flowchart: Remove a substring from a specified string

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to remove a substring from a specified string.
Next: Write a Ruby program to count the occurrences of a specified character in a given string.

What is the difficulty level of this exercise?