Home »
Ruby Tutorial »
Ruby Programs
Ruby program to get characters from a string using the index
Last Updated : December 15, 2025
Problem Solution
In this program, we will create a string variable, and then we will get the characters from the string based on the index.
Program/Source Code
The source code to get characters from a string using the index is given below. The given program is compiled and executed successfully.
# Ruby program to get characters from
# a string using the index.
Mystr = "Hello World";
index=0;
while index<Mystr.length
print Mystr[index]," ";
index=index + 1;
end
Output
H e l l o W o r l d
Explanation
In the above program, we created a string variable Mystr initialized with "Hello World". Then we traversed the string and get characters from the string one by one using index and printed them.
Ruby Strings Programs »
Advertisement
Advertisement