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

Ruby中的concat()方法相关推荐

  1. ruby中的回调方法和钩子方法

    在ruby中,当某些特定的事件发生时,将调用回调方法和钩子方法.事件有如下几种: 调用一个不存在的对象方法 类混含一个模块 定义类的子类 给类添加一个实例方法 给对象添加一个单例方法 引用一个不存在的 ...

  2. C#中string.Concat方法的使用

    string.Concat方法用于连接string的一个或多个实例,或string的一个或多个实例的object表示形式 https://docs.microsoft.com/zh-cn/dotnet ...

  3. 如何在Ruby中使用数组方法

    介绍 (Introduction) Arrays let you represent lists of data in your programs. Once you have data in an ...

  4. zemax微透镜阵列示例_阵列反向! Ruby中的示例方法

    zemax微透镜阵列示例 阵列反向! 方法 (Array reverse! Method) In this article, we will study about Array.reverse! me ...

  5. javascript学习系列(13):数组中的concat方法

    最好的种树是十年前,其次是现在.歌谣 每天一个前端小知识 提醒你改好好学习了 知乎博主 csdn博主 b站博主  放弃很容易但是坚持一定很酷     我是歌谣 喜欢就一键三连咯 你得点赞是对歌谣最大的 ...

  6. javascript学习系列(7):数组中的concat方法

    最好的种树是十年前,其次是现在.歌谣 每天一个前端小知识 提醒你改好好学习了 知乎博主 csdn博主 b站博主  放弃很容易但是坚持一定很酷     我是歌谣 喜欢就一键三连咯 你得点赞是对歌谣最大的 ...

  7. rotate array_Array.rotate! Ruby中的示例方法

    rotate array Array.rotate! 方法 (Array.rotate! Method) In this article, we will study about Array.rota ...

  8. ruby array_Array.select! Ruby中的示例方法

    ruby array Array.select! 方法 (Array.select! Method) In this article, we will study about Array.select ...

  9. mybatis mysql concat_在MyBatis中使用concat()方法

    concat介绍 CONCAT(字串1, 字串2, 字串3, ...): 将字串1.字串2.字串3,等字串连在一起. 示例 SELECT CONCAT(region_name,store_name) ...

  10. ruby中、.reject_Ruby中带有示例的Array.reject方法

    ruby中..reject Ruby Array.reject方法 (Ruby Array.reject Method) In the last article, we have seen how w ...

最新文章

  1. 星际2虫王iA加入商汤,担任AI研究员,网友:iA vs AI ,是自己训练跟自己打吗?...
  2. DynamicList
  3. mysql 创建和删除用户
  4. Windows10系统安装 .NET Framework 3.5
  5. jzoj3055-比赛【数学,统计】
  6. 微信小程序canvas绘制图片的注意事项---不能是网络图片
  7. 跳出框架iframe的操作语句
  8. 夯实Java基础(八)——代码块
  9. 如何正确在IDEA 里maven构建的项目中引入lib的jar包(图文详解)
  10. kafka 重新分配partition
  11. ios input框输入白屏
  12. 实现RedHat6.3全屏,解决最大分辨率只有800*600
  13. Python 爬虫 书籍爬取实例
  14. 二阶系统响应指标图_二阶系统的性能指标
  15. 探索性测试:常见误区
  16. 2008服务器系统开机用户名和密码忘记了,服务器2008密码忘记了
  17. VMware导入vmdk格式的文件 踩了一堆坑~~~
  18. 怎样让健康码截图合并一张图片_健康码拼图
  19. Slot-Gated Modeling for Joint Slot Filling and Intent Prediction论文笔记
  20. Mac上安装MySQL图文教程(解决了临时密码和编码集问题)

热门文章

  1. 套料软件XSuperNEST
  2. C++与QT学习路线
  3. 使用STM8S003F3P6的硬件I2C读写AT24C16的EEPROM的经验心得
  4. linux ext4 磁盘修复,修复损坏的 ext4 大分区数据
  5. **容易混淆的4中park变换**(转载)
  6. 第22篇 项目进度管理__计划评审技术__重点内容
  7. 【IT项目管理】第5章 保障项目进度
  8. 手机如何打开.html,手机怎么打开HTML
  9. 做H5页面用什么软件比较好?
  10. python 源代码剖析mobi_Python学习手册.mobi kindle电子书 带源码