日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby字符串截取字符串_如何在Ruby中附加字符串? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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實例之間執(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 :這三個是String類的實例。 Str1是將要附加的String類對象。

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.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實例之間執(zhí)行串聯(lián)。

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:

程序:

=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.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實例之間執(zhí)行加法。 添加也可以視為追加。

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.

與其他兩種方法相比,這種方法效率較低,因為您需要一個臨時存儲區(qū)來存儲生成的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:

程序:

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

總結

以上是生活随笔為你收集整理的ruby字符串截取字符串_如何在Ruby中附加字符串?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。