红宝石 |字符串 casecmp 方法
casecmp是 Ruby 中的 String 类方法,它是 String#<=> 的不区分大小写版本。目前,不区分大小写仅适用于字符AZ/az,而不适用于所有 Unicode字符。这个方法和casecmp不同!方法。
Syntax: str.casecmp(other_str)
Parameters: Here, str is the given string to be checked and other_str is the string to which str is compared.
Returns:This method will returns the true or false based on the equality of the str and other_str. It can also return nil if the two strings have incompatible encodings, or if other_str is not a string.
示例 1:
# Ruby program to demonstrate
# the casecmp method
# Taking a string and
# using the method
puts "RuBy".casecmp("ruby")
puts "GeeksforGeeks".casecmp("gfg")
输出:
0
-1
示例 2:
# Ruby program to demonstrate
# the casecmp method
# Taking a string and
# using the method
# here it will give nil
puts "\u{e5 f6 dc}".encode("ISO-8859-1").casecmp("\u{c4 d4 de}")
puts "GFG".casecmp("250")
输出:
1