Ruby | String casecmp Method
Last Updated :
08 Jan, 2020
casecmp is a String class method in Ruby which is Case-insensitive version of String#<=>. For now, case-insensitivity only works on characters A-Z/a-z, not all of the Unicode characters. This method is different from casecmp! method.
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.
Example 1:
puts "RuBy" .casecmp( "ruby" )
puts "GeeksforGeeks" .casecmp( "gfg" )
|
Output:
0
-1
Example 2:
puts "\u{e5 f6 dc}" .encode( "ISO-8859-1" ).casecmp( "\u{c4 d4 de}" )
puts "GFG" .casecmp( "250" )
|
Output:
1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...