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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540862.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540862.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540862.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

centos查找linux大文件,centos中查找出大文件命令匯總

在linux中簡單的查找文件與目錄大小很簡單#已易讀的格式顯示指定目錄或文件的大小&#xff0c;-s選項指定對于目錄不詳細顯示每個子目錄或文件的大小du -sh [dirname|filename]如&#xff1a;當前目錄的大小&#xff1a;代碼如下復制代碼du -sh .當前目錄下個文件或目錄的大小&…

WSFC真實場景仲裁處理

在本篇文章中&#xff0c;老王將從實際應用的角度來為大家講解下群集仲裁在真實情況下的呈現&#xff0c;以及出現不同點數的節點宕機應該如何處理&#xff0c;在老王本篇文章中以及以后的文章中&#xff0c;我并不會去講如何去安裝一個群集&#xff0c;后面我們也將主要專注于…

ccie是什么_CCIE的完整形式是什么?

ccie是什么CCIE&#xff1a;思科認證互聯網專家 (CCIE: Cisco Certified Internetwork Expert) CCIE is an abbreviation of the "Cisco Certified Internetwork Expert". CCIE是“ Cisco認證互聯網專家”的縮寫。 It is a technical certification, which was set …

從零開始自學c語言,從零開始學習C語言

了解指針變量&#xff0c;代碼如下&#xff1a;int main(){int a 10;//4個字節int* p &a;//取地址aprintf("%p\n",p);//有一種變量是用來存放地址的 - 指針變量printf("%p\n", &a);*p 20;//* - 解引用操作符printf("%d\n", a);return…

SFB 項目經驗-12-為某上市企業的Skype for Business購買Godday證書

《要想看Lync 2013升級SFB 2015真實項目經驗&#xff1a;請看Lync 項目經驗-01-到-Lync 項目經驗-10》本系列博文&#xff1a;Lync 項目經驗-01-共存遷移-Lync2013-TO-SFB 2015-規劃01http://dynamic.blog.51cto.com/711418/1858520 Lync 項目經驗-02-共存遷移-Lync2013-TO-SF…

在Linux中制作實用程序(MakeFile)

Hey folks, have you ever used IDEs? Most probably, yes. So whats your favorite one? Geany, CodeBlocks, DevC, Eclipse, NetBeans or something else? 大家好&#xff0c;您曾經使用過IDE嗎&#xff1f; 很有可能&#xff0c;是的。 那你最喜歡哪一個呢&#xff1f; G…

c語言單片機彩燈程序設計,用C語言實現鍵控彩燈系統

源程序&#xff1a;#include "reg51.h"#define uchar unsigned charuchar flag;uchar light,assum;void delay05s(){unsigned char i,j,k;for(i5;i>0;i--)for(j200;j>0;j--)for(k250;k>0;k--);}void delay10ms(void){unsigned char i,j;for(i20;i>0;i--)…

wdcp支持兩種安裝方式

v3.2版本已發布&#xff0c;支持多PHP版本共存共用&#xff0c;支持SSL證書&#xff0c;更多可看論壇 v3版討論區更多安裝說明請看 http://www.wdlinux.cn/bbs/thread-57643-1-1.html wdcp支持兩種安裝方式 1 源碼編譯 此安裝比較麻煩和耗時,一般是20分鐘至一個小時不等,具體視…

c語言定義5個元素數組, 對數組進行從小到大排序,定義一個5行5列的二維數組,并動態賦值,將第3列的數組進行從小到大的排序...

滿意答案xvercjdl32013.10.07采納率&#xff1a;47% 等級&#xff1a;10已幫助&#xff1a;272人#include #include int cmp(const void* a, const void* b){return *(int*)a > *(int*)b;}int main(){int arr[5][5];int tmp[5];int ct 0;int i,j;printf("input 25 …

oracle下載(轉載)

復制鏈接使用迅雷即可下載。4種包的介紹&#xff1a;***_database_*.zip 是Oracle數據庫 服務端 軟件的安裝包&#xff0c;如果你要創建一個數據庫&#xff0c;請下載這個版本***_client_*.zip 是oracle數據庫 客戶端 軟件的安裝包&#xff0c;如果你不需要創建…

c++ cdi+示例_C ++“或”關鍵字示例

c cdi示例"or" is an inbuilt keyword that has been around since at least C98. It is an alternative to || (Logical OR) operator and it mostly uses with the conditions. “ or”是一個內置關鍵字&#xff0c;至少從C 98起就存在。 它是||的替代方法 ( 邏輯…

智能循跡避障小車C語言程序編寫思路,基于單片機的智能小車紅外避障循跡系統設計與制作...

余秀玲 余秀娟摘 要&#xff1a;隨著科技的高速發展&#xff0c;人們對生活質量的要求越來越高&#xff0c;無人駕駛汽車已經被廣為研發和試用&#xff0c;由此智能小車的快速發展也是在情理之中。通過對基于單片機的智能小車的硬件及軟件設計分析&#xff0c;實現紅外避障循跡…

主板擴展槽圖解_子板:擴展到主板

主板擴展槽圖解A daughterboard is a circuit board that plugs into and extends the circuitry of the main board called motherboard. A daughterboard is connected directly to the motherboard. Unlike expansion cards, which connect with the motherboard using the …

c語言春考題目,PAT 2017年春考乙級真題(1066. 圖像過濾)(C語言)

題目原文&#xff1a;圖像過濾是把圖像中不重要的像素都染成背景色&#xff0c;使得重要部分被凸顯出來。現給定一幅黑白圖像&#xff0c;要求你將灰度值位于某指定區間內的所有像素顏色都用一種指定的顏色替換。輸入格式&#xff1a;輸入在第一行給出一幅圖像的分辨率&#xf…

NHibernate利用Mindscape.NHibernateModelDesigner實現數據庫與實體之間的轉換及操作

環境&#xff1a; &nbsp&nbspVisual Studio 2010 一、Mindscape.NhibernateModelDesigner安裝 &nbsp&nbsp在打開VS2010之后&#xff0c;我們能夠在“工具”菜單下找到“擴展管理器&#xff0c;搜索&#xff1a;Mindscape NHibernate Model Designer 下載安裝就…

樹1 樹的同構_檢查樹是否同構

樹1 樹的同構Problem statement: 問題陳述&#xff1a; Write a function to detect if two trees are isomorphic. Two trees are called isomorphic if one of them can be obtained from other by a series of flips, i.e. by swapping left and right children of a numbe…

《SEO的藝術(原書第2版)》——第1章 搜索:反映認知、連接商務

第1章 搜索&#xff1a;反映認知、連接商務 搜索已經與當今的社會融為一體。截至2011年8月&#xff0c;全球每個月執行的搜索超過了1580億次&#xff0c;每天大約執行52億次。這意味著&#xff0c;每秒平均要執行大約61 000次搜索。此外&#xff0c;用戶對搜索查詢返回的期望時…

android 動態contextmenu,在Android中使用ContextMenu與ListView

要從選定的ListView項中獲取該項&#xff0c;請參考ContextMenuInfo對象(請參見下面的最后一個實現方法)。完整解決方案如下&#xff1a;1)在ListActivity類中為上下文菜單注冊ListViewOverridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstance…

《Android應用開發攻略》——2.2 異常處理

2.2 異常處理 Ian Darwin2.2.1 問題Java有一個精心定義的異常處理機制&#xff0c;但是需要花費一定的時間學習&#xff0c;才能高效地使用而不至于使用戶或者技術支持人員感到沮喪。2.2.2 解決方案Java提供了一個Exception層次結構&#xff0c;正確地使用它能夠帶來相當大的靈…

android 默認瀏覽器 視頻播放 二維碼,Android調用系統默認瀏覽器訪問的方法

一、啟動android默認瀏覽器這樣子&#xff0c;android就可以調用起手機默認的瀏覽器訪問。二、指定相應的瀏覽器訪問1、指定android自帶的瀏覽器訪問( “com.android.browser”&#xff1a;packagename &#xff1b;“com.android.browser.BrowserActivity”&#xff1a;啟動主…