Case-insensitive Array#include?

后端 未结 6 1054
小蘑菇
小蘑菇 2020-12-13 12:35

I want to know what\'s the best way to make the String.include? methods ignore case. Currently I\'m doing the following. Any suggestions? Thanks!



        
6条回答
  •  天命终不由人
    2020-12-13 12:53

    You can use casecmp to do your comparison, ignoring case.

    "abcdef".casecmp("abcde")     #=> 1
    "aBcDeF".casecmp("abcdef")    #=> 0
    "abcdef".casecmp("abcdefg")   #=> -1
    "abcdef".casecmp("ABCDEF")    #=> 0
    

提交回复
热议问题