w3resource

Ruby String: Exercises, Practice, Solution

Ruby String Exercises[18 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

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

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.

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.

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.

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.

Sample Output:

  
JavaScript
  

Click me to see the solution

6. Write a Ruby program to remove last specified characters from a given string.
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.
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.
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.
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.
Sample Output:

1
2
1

Click me to see the solution

11. Write a Ruby program to sort a string's characters alphabetically.
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.
Sample Output:

JvScript
Pthon
PP

Click me to see the solution

13. Write a Ruby program to trim specific characters from a string.
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.
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.
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.
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.
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.
Sample Output:

JavaScript
PHP

Click me to see the solution

Ruby Code Editor:



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/index.php