Q:

Ruby program to get a substring from the string

belongs to collection: Ruby Strings Programs

0

In this program, we will create a string variable and then we will get the substring from created string using a specified range index.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to get a substring from the string is given below. The given program is compiled and executed successfully.

# Ruby program to get a substring 
# from the string

Mystr = "Hello World";
SubStr= Mystr[2..7];

print "Mystr: ",Mystr,"\n";
print "SubStr: ",SubStr,"\n";

Output:

Mystr: Hello World
SubStr: llo Wo

Explanation:

In the above program, we created a string variable Mystr initialized with "Hello World". Then we got the substring using a specified range index and assigned it to the Substr variable. After that, we printed both variables.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Strings Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to demonstrate the example to write d... >>
<< Ruby program to get characters from a string using...