w3resource

Ruby String Exercises: Get a substring from a specified position to the last char of a given string

Ruby String: Exercise-15 with Solution

Write a Ruby program to get a substring from a specified position to the last char of a given string.

Ruby String Exercises:  Get a substring from a specified position to the last char of a given string

Ruby Code:

def check_string(str, n)
   str = str[n...-1]
   return str
end
print check_string("JavaScript", 5)
print "\n",check_string("Python",3)
print "\n",check_string("Ruby", 2)
print "\n",check_string("PHP", 0)

Output:

crip
ho
b
PH

Flowchart:

Flowchart: Get a substring from a specified position to the last char of a given string

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to remove the first and last two characters from a specified string.
Next: Write a Ruby program to get the number of lines in a given string.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.