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.
Str1 , Str2和Str3 :這三個是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.
在上面的代碼中,您可以觀察到我們有三個字符串,分別是Str1 , Str2和Str3 。 我們借助于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.
Str1 , Str2和Str3 :這三個是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.
在上面的代碼中,您可以觀察到我們有三個字符串,分別是Str1 , Str2和Str3 。 我們借助<<運算符將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.
Str1 , Str2和Str3 :這三個是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.
在上面的代碼中,您可以觀察到我們有三個字符串,分別是Str1 , Str2和Str3 。 我們借助<<運算符將Str2追加到Str1中 。 我們將附加的String存儲在Str3容器中。
翻譯自: https://www.includehelp.com/ruby/how-to-append-a-string-in-ruby.aspx
ruby字符串截取字符串