ruby字符串连接
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.
加号(+)运算符通常用于将两个整数相加,但是在String对象的情况下会被覆盖。 对于字符串对象,可以在Ruby中使用+运算符添加两个字符串。 当使用+运算符时,执行速度不会变慢,因为结果字符串存储在存储它的字符串对象的新内存中。
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.
现在,让我们在下面提供的一些支持Ruby代码的帮助下理解这个概念。
Example 1:
范例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.
在上面的代码中,您可以看到我们正在借助plus(+)运算符进行字符串连接 。 我们只是将循环变量连接到旧字符串的右侧。
Example 2:
范例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.
在上面的代码中,您可以观察到我们正在使用plus(+)运算符进行字符串连接,但是在这里,不是将结果字符串存储在old_string中 ,而是将其存储在new_string中 ,并借助puts ()方法,我们正在打印它。
Example 3:
范例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.
在上面的代码中,您可以看到我们也可以借助+运算符将字符串或字符串对象添加到左侧。
翻译自: https://www.includehelp.com/ruby/concatenate-strings-using-plus-operator-in-ruby.aspx
ruby字符串连接