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:借助預(yù)定義方法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類的預(yù)定義方法。 此方法用于在兩個(gè)String實(shí)例之間執(zhí)行串聯(lián)。
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.
Str1 , Str2和Str3 :這三個(gè)是String類的實(shí)例。 Str1是將要附加的String類對(duì)象。
Program:
程序:
=beginRuby program to append a String with the help of concat() method. =endStr1 = "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.comExplanation:
說明:
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.
在上面的代碼中,您可以觀察到我們有三個(gè)字符串,分別是Str1 , Str2和Str3 。 我們借助于String.concat方法將Str2附加到Str1中 。 我們將附加的String存儲(chǔ)在Str3容器中。
Method 2: With the help of << operator
方法2:在<<運(yùn)算符的幫助下
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類的預(yù)定義方法/運(yùn)算符。 此方法用于在兩個(gè)String實(shí)例之間執(zhí)行串聯(lián)。
Syntax:
句法:
String1 << String2Variables 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.
Str1 , Str2和Str3 :這三個(gè)是String類的實(shí)例。 Str1是將要附加的String類對(duì)象。
Program:
程序:
=beginRuby program to append a String with the help of << operator. =endStr1 = "Includehelp" Str2 = ".com"Str3 = Str1<<Str2puts "The appended String object is #{Str3}"Output
輸出量
The appended String object is Includehelp.comExplanation:
說明:
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.
在上面的代碼中,您可以觀察到我們有三個(gè)字符串,分別是Str1 , Str2和Str3 。 我們借助<<運(yùn)算符將Str2追加到Str1中 。 我們將附加的String存儲(chǔ)在Str3容器中。
Method 3: With the help of + operator
方法3:在+運(yùn)算符的幫助下
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類的預(yù)定義方法/運(yùn)算符。 此方法用于在兩個(gè)String實(shí)例之間執(zhí)行加法。 添加也可以視為追加。
Syntax:
句法:
String3 = String1 + String2This 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.
與其他兩種方法相比,這種方法效率較低,因?yàn)槟枰粋€(gè)臨時(shí)存儲(chǔ)區(qū)來存儲(chǔ)生成的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.
Str1 , Str2和Str3 :這三個(gè)是String類的實(shí)例。 Str1是將要附加的String類對(duì)象。
Program:
程序:
=beginRuby program to append a String with the help of + operator. =endStr1 = "Includehelp" Str2 = ".com"Str3 = Str1 + Str2puts "The appended String object is #{Str3}"Output
輸出量
The appended String object is Includehelp.comExplanation:
說明:
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.
在上面的代碼中,您可以觀察到我們有三個(gè)字符串,分別是Str1 , Str2和Str3 。 我們借助<<運(yùn)算符將Str2追加到Str1中 。 我們將附加的String存儲(chǔ)在Str3容器中。
翻譯自: https://www.includehelp.com/ruby/how-to-append-a-string-in-ruby.aspx
ruby字符串截取字符串
總結(jié)
以上是生活随笔為你收集整理的ruby字符串截取字符串_如何在Ruby中附加字符串?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 8086简单的指令流水线_在8086微处
- 下一篇: [转载] Java Formatter
