ruby字符串截取字符串_如何在Ruby中附加字符串?

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字符串截取字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值