w3resource
w3resource logo
Ruby String Exercises

Ruby String: Exercises, Practice, Solution

Ruby String Exercises[18 exercises with solution]

1. Write a Ruby program to draw a string as bold or italic text. Go to the editor

Sample Output:

<i> Ruby </i>
<b> Ruby </b>

Click me to see the solution

2. Write a Ruby program to insert a string of length 2 to an another string where the first string will be in the middle of the second string. Go to the editor

Sample Output:

Python
Pythonz
Pyothnxz

Click me to see the solution

3. Write a Ruby program to lower case, upper case and capitalizes all the words of a given string. Go to the editor

Sample Output:

RUBY EXERCISES!
ruby exercises!
Ruby exercises!

Click me to see the solution

4. Write a Ruby program to check whether first and the last element are the same of an given array of integers. The array length must be 1 or more. Go to the editor

Sample Output:

Substring present in the string.
Substring not present in the string.

Click me to see the solution

5. Write a Ruby program to remove all white space within a string. Go to the editor

Sample Output:

  
JavaScript
  

Click me to see the solution

6. Write a Ruby program to remove last specified characters from a given string. Go to the editor
Sample Output:

JavaScrip
JavaScri
JavaScr
JavaSc
JavaS

Click me to see the solution

7. Write a Ruby program to split a delimited string and convert it to an array. Go to the editor
Sample Output:

["A", "B", "C", "D", "E", "F"]
["A", "B", "C", "D", "E", "F"]

Click me to see the solution

8. Write a Ruby program to remove a substring from a specified string. Go to the editor
Sample Output:

Java
CSS

Click me to see the solution

9. Write a Ruby program to check whether a string starts with an other string. Go to the editor
Sample Output:

true
false

Click me to see the solution

10. Write a Ruby program to count the occurrences of a specified character in a given string. Go to the editor
Sample Output:

1
2
1

Click me to see the solution

11. Write a Ruby program to sort a string's characters alphabetically. Go to the editor
Sample Output:

aacijprstv
hnopty
HPP

Click me to see the solution

12. Write a Ruby program to create a new array with the elements in reverse order from a given an array of integers length 3. Go to the editor
Sample Output:

JvScript
Pthon
PP

Click me to see the solution

13. Write a Ruby program to trim specific characters from a string. Go to the editor
Sample Output:

JvScript
Pthon
avacript

Click me to see the solution

14. Write a Ruby program to remove the first and last two characters from a specified string. Go to the editor
Sample Output:

vaScri
th

Click me to see the solution

15. Write a Ruby program to get a substring from a specified position to the last char of a given string. Go to the editor
Sample Output:

crip
ho
b
PH

Click me to see the solution

16. Write a Ruby program to get the number of lines in a given string. Go to the editor
Sample Output:

Number of lines of the above string: 6

Click me to see the solution

17. Write a Ruby program to truncate a given string to the first n words. Go to the editor
Sample Output:

Python is a
Python is a widely used high-level, general-purpose, interpreted,
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.

Click me to see the solution

18. Write a Ruby program to remove a character from a given string if it starts with that specified character. Go to the editor
Sample Output:

JavaScript
PHP

Click me to see the solution

Ruby Code Editor: