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>
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
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!
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.
5. Write a Ruby program to remove all white space within a string.
Sample Output:
JavaScript
6. Write a Ruby program to remove last specified characters from a given string.
Sample Output:
JavaScrip JavaScri JavaScr JavaSc JavaS
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"]
8. Write a Ruby program to remove a substring from a specified string.
Sample Output:
Java CSS
9. Write a Ruby program to check whether a string starts with an other string.
Sample Output:
true false
10. Write a Ruby program to count the occurrences of a specified character in a given string.
Sample Output:
1 2 1
11. Write a Ruby program to sort a string's characters alphabetically.
Sample Output:
aacijprstv hnopty HPP
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
13. Write a Ruby program to trim specific characters from a string.
Sample Output:
JvScript Pthon avacript
14. Write a Ruby program to remove the first and last two characters from a specified string.
Sample Output:
vaScri th
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
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
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.
18. Write a Ruby program to remove a character from a given string if it starts with that specified character.
Sample Output:
JavaScript PHP
Ruby Code Editor:
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/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics