To append a string to another string in Ruby, you can use the +
operator or the concat
method.
In this example,
str1
with a value of "Hello"
.+
operator to append the string " World"
to str1
.+
operator concatenates the two strings and returns the result.str1 = "Hello"
concatenated = str1 + " World"
puts concatenated
Hello World
In this example,
str2
with a value of "banana"
.concat
method to append the string "apple"
to str2
.concat
method concatenates the two strings and modifies the original str2
.str2 = "banana"
str2.concat("apple")
puts str2
bananaapple
In this tutorial, we learned How to Append a String to another String in Ruby language with well detailed examples.