📜  红宝石 |切弦法

📅  最后修改于: 2022-05-13 01:54:27.089000             🧑  作者: Mango

红宝石 |切弦法

Chop是 Ruby 中的 String 类方法,用于返回删除最后一个字符的新字符串。如果字符串以 \r\n, b 结尾,则删除这两个字符。将chop 应用于空字符串会返回一个空字符串。

示例 1:

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


Ruby
# 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


输出:

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