Home » Ruby programming

String concatenation in Ruby

Ruby string concatenation: Here, we are going to learn about the concatenating the strings using the various methods in Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on September 12, 2019

Most of the times, you may need to concatenate two or more strings. Concatenation simply means that joining two or more strings together. The result may be stored in the actual string or a new memory can be allocated to the new string as per the requirement of the program. Ruby provides you several ways through which you can concatenate two or more than two strings at a time. The result given by them is the same but they may differ in execution speed. Let us go through each of them and see their examples for a better understanding of the concept.

1) Using concat() method

Using concat() method makes the execution faster as it works and stores the string in the same object. Refer the example given below:

=begin
Ruby program to concat strings using concat method.
=end

puts "Enter the string:"
str = gets.chomp

for i in 0..15
	str.concat(i.to_s)
end

puts "The string is #{str}"

Output

Hrithik
The string is Hrithik0123456789101112131415

In the above code, you can observe that we are creating an object named str and calling concat() method with it. We are adding the value of the loop variable i by converting its value to string using .to_s method because it is necessary to provide string to the concat() method because it returns string. If you don't provide a string value and in turn providing an integer value, then it will consider it as an ASCII code and will return the corresponding symbol.

2) Using + operator

You can also carry out the process of concatenation with the help of the + operator. Refer the example given below:

=begin
Ruby program to concat strings using + operator.
=end

puts "Enter the string:"
str = gets.chomp

for i in 0..15
	str=str+i.to_s
end

puts "The string is #{str}"

Output

Hrithik
The string is Hrithik0123456789101112131415

3) Using << operator (Appending)

Concatenation of two or more strings can also be carried out using the << operator. It also stores the result in the same object. Refer the example given below:

=begin
Ruby program to concat strings using << operator.
=end

puts "Enter the string:"
str = gets.chomp

for i in 0..15
	str<<i.to_s
end

puts "The string is #{str}"

Output

Hrithik
The string is Hrithik0123456789101112131415

4) Using string interpolation

You must have used the #{} symbol for printing the value of a variable using the puts() method. You are doing nothing but just concatenates multiple strings and ultimately printing a new string. Refer the example given below:

=begin
Ruby program to concat strings using 
string interpolation.
=end

puts "Enter the string:"
str = gets.chomp

for i in 0..15
	str="#{str}#{i}"
end

puts "The string is #{str}"

Output

Hrithik
The string is Hrithik0123456789101112131415

In the above example, you can see that, if you want to join the value of loop variable i, you don't need to convert into string using .to_s method. It ultimately shows that string interpolation converts any variable value into string implicitly.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.