红宝石 |字符串删除后缀!方法
删除后缀!是 Ruby 中的 String 类方法,用于返回给定字符串的副本,如果没有进行任何更改,则删除尾随后缀或 nil。
Syntax:str.delete_suffix!
Parameters: Here, str is the given string.
Returns: A copy of the given string with trailing suffix deleted or nil if no change was made.
示例 1:
# Ruby program to demonstrate
# the delete_suffix! method
# Taking a string and
# using the method
puts "Ruby".delete_suffix!("by")
puts "Class".delete_suffix!("s")
输出:
Ru
Clas
示例 2:
# Ruby program to demonstrate
# the delete_suffix! method
# Taking a string and
# using the method
puts "Sample".delete_suffix!("pl")
puts "Input".delete_suffix!("ut")
输出:
Inp