當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby String chop用法及代碼示例

chop是Ruby中的String類方法,用於返回刪除了最後一個字符的新String。如果字符串以\ r \ n,b結尾,則兩個字符都將被刪除。將chop應用於空字符串將返回一個空字符串。

用法:str.chop

參數:在這裏,str是給定的字符串。


返回值:沒有記錄分隔符的新字符串。

示例1:

# Ruby program to demonstrate  
# the chop method  
       
# Taking a string and  
# using the method  
puts "Ruby".chop 
puts "Ruby\r\n".chop 

輸出:

Rub
Ruby

示例2:

# Ruby program to demonstrate  
# the chop method  
       
# Taking a string and  
# using the method  
puts "String\r\n\r\r\n".chop 
  
# Removing two characters  
puts "Method".chop.chop

輸出:

String

Meth


相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | String chop Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。