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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/string/ruby-string-exercise-18.php