w3resource

Ruby String Exercises: Remove a character from a given string if it starts with that specified character

Ruby String: Exercise-18 with Solution

Write a Ruby program to remove a character from a given string if it starts with that specified character.

Ruby String Exercises: Remove a character from a given string if it starts with that specified character

Ruby Code:

my_string = "JJavaScript"
print my_string.sub!(/^J/, '')
my_string = "PPHP"
print "\n",my_string.sub!(/^P/, '')

Output:

JavaScript
PHP

Flowchart:

Flowchart: Remove a character from a given string if it starts with that specified character

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to truncate a given string to the first n words.
Next: Ruby Exercise

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.