ruby字符串连接_Ruby中的字符串连接

ruby字符串连接

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.

大多数时候,您可能需要连接两个或多个字符串串联只是意味着将两个或多个字符串连接在一起。 结果可以存储在实际的字符串中,也可以根据程序要求将新的内存分配给新的字符串。 Ruby为您提供了几种可以一次连接两个或两个以上字符串的方式。 它们给出的结果相同,但是执行速度可能不同。 让我们逐一讨论它们,并查看它们的示例,以更好地理解该概念。

1)使用concat()方法 (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:

使用concat()方法可加快执行速度,并将其存储在同一对象中。 请参考以下示例:

=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.

在上面的代码中,您可以观察到我们正在创建一个名为str的对象,并使用它调用concat()方法 。 我们通过使用.to_s方法将其值转换为字符串来添加循环变量i的值,因为有必要向concat()方法提供字符串,因为它会返回字符串。 如果您不提供字符串值,而是提供整数值,则它将认为它是ASCII码 ,并返回相应的符号。

2)使用+运算符 (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)使用<<运算符(追加) (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)使用字符串插值 (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:

您必须使用#{}符号来使用puts()方法打印变量的值。 您什么也不做,只是连接多个字符串并最终打印一个新字符串。 请参考以下示例:

=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.

在上面的示例中,您可以看到,如果要加入循环变量i的值,则无需使用.to_s方法转换为字符串。 最终表明,字符串插值将任何变量值隐式转换为字符串。

翻译自: https://www.includehelp.com/ruby/string-concatenation-in-ruby.aspx

ruby字符串连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值