红宝石 |断弦!方法
劈!是 Ruby 中的 String 类方法,用于返回删除最后一个字符的新字符串。如果字符串以 \r\n, b 结尾,则删除这两个字符。如果字符串为空,它将返回 nil。
Syntax:str.chop!
Parameters: Here, str is the given string.
Returns: A new string having no record separator or nil if the string is empty.
示例 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