📜  红宝石 |断弦!方法

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

红宝石 |断弦!方法

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

示例 1:

Ruby
# Ruby program to demonstrate
# the chop! method
      
# Taking a string and
# using the method
puts "".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!


输出:

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
Method