Q:

Ruby program to demonstrate the string concatenation

belongs to collection: Ruby Strings Programs

0

In this program, we will demonstrate the different ways of string concatenation and print the string after concatenation.

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 demonstrate the string concatenation is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# string concatenation.

# Concatenate using "+" operator.
MyStr1 = "Hello " + "World";   
puts MyStr1;

# Concatenate using a single space.  
MyStr2 = "Hello " "World";   
puts MyStr2;
  
# Concatenate using "<<" operator.
MyStr3 = "Hello " << "World";   
puts MyStr3;
  
# Concatenate using concat() method.
MyStr4 = "Hello ".concat("World");
puts MyStr4;

Output:

Hello World
Hello World
Hello World
Hello World

Explanation:

In the above program, we created string variables and assigned values by concatenating strings using different ways and printed the result.

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 make string immutable... >>
<< Ruby program to demonstrate the escape sequence ch...