Home » Ruby programming

How to concatenate strings using plus (+) operator in Ruby?

Concatenating string using plus (+) operator: Here, we are going to learn how to concatenate the string using the plus (+) operator in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on September 14, 2019

Concatenating two or more strings with the help of plus (+) operator is the most basic approach once can apply to fulfil the task. We have got various other ways but using this makes the program easier to understand.

The plus (+) operator is generally used to add two integers but it is overridden in the case of String objects. In the case of string object, you can add two strings with the help of + operator in Ruby. The execution get little slow when + operator is used because the resultant string is stored in the new memory of the string object storing it.

When you follow this approach of concatenation, you can add the string object or string on the right hand side as well as at the left hand side as per the requirement but this is not the case of other methods.

You are supposed to follow the given syntax in order to use the + operator for concatenation.

    new_str or old_str = new_str + "str" or str_object

Now let us understand this concept with the help of some supporting Ruby codes which are given below.

Example 1:

=begin
Ruby program to show the implementation 
of + operator
=end

puts "Enter any string"
str = gets.chomp

for i in 1..10
	str = str + i.to_s
end

puts "The resultant string is : #{str}"

Output

Enter any string
Ranumondalterimeri
The resultant string is : Ranumondalterimeri12345678910

Explanation:

In the above code, you can observe that we are carrying out string concatenation with the help of the plus (+) operator. We are just concatenating loop variable to the right-hand side of the old string.

Example 2:

=begin
Ruby program to show the implementation 
of + operator
=end

puts "Enter any string"
str = gets.chomp

new_str = str + "\t" + "Nothing"

puts "The resultant string is : #{new_str}"

Output

Enter any string
Gita
The resultant string is : Gita  Nothing

Explanation:

In the above code, you can observe that we are carrying out string concatenation with the help of plus (+) operator but here, instead of storing the resultant string in the old_string, we are storing it into a new_string and with the help of puts() method, we are printing it.

Example 3:

=begin
Ruby program to show the implementation 
of + operator
=end

puts "Enter any string"
str = gets.chomp

new_str = "Nothing " + str

puts "The resultant string is : #{new_str}"

Output

Enter any string
Ranu
The resultant string is : Nothing Ranu

Explanation:

In the above code, you can observe that we can add string or string object to the left-hand side also with the help of the + operator.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.