w3resource

Ruby String Exercises: Check whether a string starts with an other string

Ruby String: Exercise-9 with Solution

Write a Ruby program to check whether a string starts with an other string.

Ruby String Exercises: Check whether a string starts with an other string

Ruby Code:

def check_string(my_string, substr)
   return my_string.start_with?(substr)
end
print check_string("JavaScript", "Java")
print "\n",check_string("Python", "PHP")

Output:

true
false

Flowchart:

Flowchart: Check whether a string starts with an other 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?



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://w3resource.com/ruby-exercises/string/ruby-string-exercise-9.php