ruby字符串截取字符串

There are multiple ways to do the required but we will study about three of them.

有多种方法可以满足要求,但我们将研究其中的三种方法。

Method 1: With the help of predefined method concat()

方法1:借助预定义方法concat()

The concat() is a predefined method for String class in Ruby's library. This method is used for carrying out concatenation between two String instances.

concat()是Ruby库中String类的预定义方法。 此方法用于在两个String实例之间执行串联。

Syntax:

句法:

    String1.concat(String2)

Variables used:

使用的变量:

Str1, Str2 and Str3: The three are the instances of String class. Str1 is the String class object which is going to be appended.

Str1Str2Str3 :这三个是String类的实例。 Str1是将要附加的String类对象。

Program:

程序:

=begin
Ruby program to append a String with the
help of concat() method.
=end
Str1 = "Includehelp"
Str2 = ".com"
puts "Str1 is #{Str1}"
puts "Str2 is #{Str2}"
Str3 = Str1.concat(Str2)
puts "The appended String object is #{Str3}"

Output

输出量

Str1 is Includehelp
Str2 is .com
The appended String object is Includehelp.com

Explanation:

说明:

In the above code, you can observe that we have three strings namely Str1, Str2, and Str3. We are appending Str2 into Str1 with the help of String.concat method. We are storing the appended String in Str3 container.

在上面的代码中,您可以观察到我们有三个字符串,分别是Str1Str2Str3 。 我们借助于String.concat方法将Str2附加到Str1中 。 我们将附加的String存储在Str3容器中。

Method 2: With the help of << operator

方法2:在<<运算符的帮助下

The << is a predefined method/operator for String class in Ruby's library. This method is used for carrying out concatenation between two String instances.

<<是Ruby库中String类的预定义方法/运算符。 此方法用于在两个String实例之间执行串联。

Syntax:

句法:

    String1 << String2

Variables used:

使用的变量:

Str1, Str2, and Str3: The three are the instances of String class. Str1 is the String class object which is going to be appended.

Str1Str2Str3 :这三个是String类的实例。 Str1是将要附加的String类对象。

Program:

程序:

=begin
Ruby program to append a String with the
help of << operator.
=end
Str1 = "Includehelp"
Str2 = ".com"
Str3 = Str1<<Str2
puts "The appended String object is #{Str3}"

Output

输出量

    The appended String object is Includehelp.com

Explanation:

说明:

In the above code, you can observe that we have three strings namely Str1, Str2, and Str3. We are appending Str2 into Str1 with the help of << operator. We are storing the appended String in Str3 container.

在上面的代码中,您可以观察到我们有三个字符串,分别是Str1Str2Str3 。 我们借助<<运算符将Str2追加到Str1中 。 我们将附加的String存储在Str3容器中。

Method 3: With the help of + operator

方法3:在+运算符的帮助下

The + is a predefined method/operator for String class in Ruby's library. This method is used to carry out addition between two String instances. Adding can be considered as appending as well.

+是Ruby库中String类的预定义方法/运算符。 此方法用于在两个String实例之间执行加法。 添加也可以视为追加。

Syntax:

句法:

    String3 = String1 + String2

This way is less efficient in comparison with other two because you need to have a temporary storage to store the resultant String which is not the case of concat() and << method.

与其他两种方法相比,这种方法效率较低,因为您需要一个临时存储区来存储生成的String,而不是concat()和<<方法。

Variables used:

使用的变量:

Str1, Str2 and Str3: The three are the instances of String class. Str1 is the String class object which is going to be appended.

Str1Str2Str3 :这三个是String类的实例。 Str1是将要附加的String类对象。

Program:

程序:

=begin
Ruby program to append a String with
the help of + operator.
=end
Str1 = "Includehelp"
Str2 = ".com"
Str3 = Str1 + Str2
puts "The appended String object is #{Str3}"

Output

输出量

The appended String object is Includehelp.com

Explanation:

说明:

In the above code, you can observe that we have three strings namely Str1, Str2, and Str3. We are appending Str2 into Str1 with the help of << operator. We are storing the appended String in Str3 container.

在上面的代码中,您可以观察到我们有三个字符串,分别是Str1Str2Str3 。 我们借助<<运算符将Str2追加到Str1中 。 我们将附加的String存储在Str3容器中。

翻译自: https://www.includehelp.com/ruby/how-to-append-a-string-in-ruby.aspx

ruby字符串截取字符串

ruby字符串截取字符串_如何在Ruby中附加字符串?相关推荐

  1. python字符串筛选输出_如何在Python中过滤字符串列表

    Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...

  2. python字符串转浮点数_如何在Python中检查字符串是否为数字(浮点数)?

    python字符串转浮点数 Using python it is very to interconvert the datatypes of a variable. A string can be e ...

  3. php根据字符串分割字符串_如何在PHP中按字符串分割字符串?

    php根据字符串分割字符串 How to split a string by string in PHP? For example, 如何在PHP中按字符串分割 字符串 ? 例如, "a s ...

  4. python输入一串字符串、输出字符串中的数字_Python 字符串与数字输出方法 如何在python中输入字符串,输出十进制...

    python将字符串中的数字乘以2并输出整个字符串 2018 Amazon Jeff Bezos 1120 4036 Amazon Jeff Bezos 2240 def db(num): impor ...

  5. java字符串字符排列组合_如何在Java中查找字符串的所有排列

    java字符串字符排列组合 In this tutorial, we will learn how to find the permutation of a String in a Java Prog ...

  6. mysql中转换成字符串_如何在R中转换字符串的大小写?

    mysql中转换成字符串 Hello, folks. In this tutorial we are going to convert the case of the string in R. The ...

  7. golang 格式化字符串_如何在Go中格式化字符串

    golang 格式化字符串 As strings are often made up of written text, there are many instances when we may wan ...

  8. python3提取字符串中的数字_如何在Python中从字符串中提取数字?

    14 回复 | 直到 1 年前 1 430 3 年前 如果只想提取正整数,请尝试以下操作: >>> str = "h3110 23 cat 444.4 rabbit 11 ...

  9. java中如何检查字符串都是数字_如何在Java中检查字符串是否为数字?

    我们将检查字符串是否为数字-借助逻辑,我们将解决此问题,第一步,我们将使用一个名为str的字符串变量,并将任何值存储在其中. 在第二步中,我们将使用一个名为str_numeric的布尔变量,该变量存储 ...

  10. scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

    scala字符串替换 Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string) Repla ...

最新文章

  1. 用WinForm/WPF代码来为.NET Core 3.0功能投票
  2. k8s和harbor的集成_爱威尔-基于kubernetes集群的项目持续集成(gitlab+harbor+Jenkins)安装...
  3. 关于Javascript框架的神回帖,值得围观
  4. 使用opencv和python进行智能图像处理_使用OpenCV在Python中进行图像处理
  5. ASP.NET服务器控件刷新后仍旧保持滚动位置
  6. HTTPS客户端验证服务器端的过程
  7. 基于OpenCv的人脸识别(Python完整代码)
  8. 汽车超人:从电商到汽后新零售的华丽转身
  9. Justinmind破解
  10. licecap图片区域问题
  11. Java程序在结构上的特点_下面关于JavaApplication程序结构特点描述中,错误的是()...
  12. s7五杀大数据英雄_王者七大记录,五杀最多的英雄竟然有他?这英雄跑的比关羽还快!...
  13. Lab3-实现计划项app辅助类实现
  14. stm32复位引脚NRST
  15. 长春有学计算机的中专吗,长春比较好的中专学校
  16. 魔兽重置版改键+喊话
  17. 一个有用的数据库:中国研究数据服务平台(CNRDS)
  18. 利用Hudi Bootstrap转化现有Hive表的parquet/orc文件为Hudi表
  19. 使用ARD远程管理用户的Parental Control和Dock
  20. macOS Command - xattr

热门文章

  1. python拟合函数_Python-最小二乘法曲线拟合
  2. elementui的upload组件怎么获取上传的文本流、_抖音feed流直播间引流你还不会玩?实操讲解...
  3. [2021-ICCV] MUSIQ Multi-scale Image Quality Transformer 论文简析
  4. pm2 start 带参数_3款有海景天窗的国产SUV,最适合带女朋友看星星,首付3万拿下...
  5. dj打碟怎么学_学DJ要不要去培训学校?
  6. .net mysql查询数据库连接_asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)...
  7. apache启动失败_请检查相关配置.√mysql5.1已启动._1、Apache启动失败,请检查相关配置-百度经验...
  8. dubbo全局异常处理_基于spring aop的dubbo异常统一处理
  9. MongoDB副本集、分片集的伪分布式部署(保姆级教程)
  10. 查找指定日期数据所在分区数据