Ruby中的concat()方法

Ruby concat()方法 (Ruby concat() method)

Ruby provides you various ways through which you can join two or more strings and store it into another memory for future utilization. The concat() method is also one of them. It provides you a way through which you can join two or more string objects and store the result into the same memory. You can even store the resultant string object as well into another string object by assigning a value to it.

Ruby提供了多种方式,您可以通过它们结合两个或更多字符串并将其存储到另一个内存中,以备将来使用。 concat()方法也是其中之一。 它为您提供了一种方法,通过该方法可以联接两个或更多字符串对象并将结果存储到同一内存中。 您甚至可以通过为其分配值来将结果字符串对象存储到另一个字符串对象中。

The concat() method is considered as one of the fastest ways to carry out concatenation as it makes use of the same object to store the resultant string object. It makes the execution faster as compared to other ways of string concatenation.

concat()方法被认为是执行连接的最快方法之一,因为它利用相同的对象来存储结果字符串对象。 与其他字符串连接方式相比,它使执行速度更快。

It takes a string object or normal string as a parameter. If an integer value is provided to it, it implicitly converts it into the corresponding character depending upon its ASCII code.

它以字符串对象或普通字符串作为参数。 如果提供给它一个整数值,它将根据其ASCII码隐式地将其转换为相应的字符。

The concat() method is implemented in the following way:

concat()方法以以下方式实现:

    String_Object.concat (String_Object)

Now let us go through its implementation in Ruby codes for a better understanding of the method.

现在,让我们看一下它在Ruby代码中的实现,以更好地理解该方法。

Example 1:

范例1:

=begin
Ruby program to concat strings 
using concat() method
=end

puts "Enter Any String"
str = gets.chomp

for i in 1..10
	str.concat(rand(0..i).to_s);
end

puts "String with random numbers : #{str}"

Output

输出量

RUN 1:
Enter Any String
Cat
String with random numbers : Cat1101544150

RUN 2:
Enter Any String
Dog
String with random numbers : Dog1233522008

Explanation:

说明:

In the above code, we are making use of the concat() method to add random numbers (which are generated by rand() method) with the string str. We have to convert the random number into string otherwise it will return the character associated with the ASCII value.

在上面的代码中,我们利用concat()方法添加带有字符串str的随机数(由rand()方法生成)。 我们必须将随机数转换为字符串,否则它将返回与ASCII值关联的字符。

Example 2:

范例2:

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

puts "Enter 10 names of the students"
str = ""

for i in 1..10
	puts " #{i} Enter name:"
	nam = gets.chomp
	str.concat(nam.concat(" "))
end

puts "The names are #{str}"

Output

输出量

Enter 10 names of the students
 1 Enter name:
Harish
 2 Enter name:
Kranthi
 3 Enter name:
j.Ai
 4 Enter name:
Vibha
 5 Enter name:
Namita
 6 Enter name:
Rajiv
 7 Enter name:
Vibha
 8 Enter name:
Nityasha
 9 Enter name:
Riya
 10 Enter name:
Somya
The names are Harish Kranthi j.Ai Vibha Namita Rajiv Vibha Nityasha Riya Somya

Explanation:

说明:

In the above code, we are calling the concat() method twice. One concat() method is being called under another concat() method. We are taking multiple names from the user inside the loop. In the last, we are printing the string object which has all the values entered by the user.

在上面的代码中,我们两次调用concat()方法。 一个concat()方法在另一个concat()方法下被调用。 我们正在循环中从用户那里获取多个名称。 最后,我们将打印具有用户输入的所有值的字符串对象。

翻译自: https://www.includehelp.com/ruby/concat-method-in-ruby.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值