compare strings with the <=> method : String comparisons « String « Ruby






compare strings with the <=> method


It compares the character code values of the strings, 
returning -1 (less than), 
          0 (equals), or 
          1 (greater than), depending on the comparison, which is case-sensitive

"a" <=> "a" # => 0
"a" <=> 97.chr # => 0
"a" <=> "b" # => -1
"a" <=> "'" # => 1

 








Related examples in the same category

1.Comparing Strings
2.perform "greater than" and "less than" comparisons:
3.A case-insensitive comparison is possible with casecmp, which has the same possible results as <=> (-1, 0, 1)
4.Comparing With Regular Expressions
5.string contains lowercase characters
6.string contains uppercase characters
7.string contains mixed case
8.string starts with a capital letter
9.Text Manipulation