Home > other >  find a specific word in a string, case intensive - ruby
find a specific word in a string, case intensive - ruby

Time:09-15

I am new to ruby, I am trying to write a method that checks if the word includes "hello" case insensitive e.g "HelLO" would still be true.

CodePudding user response:

I think the easiest approach is to downcase both string and do the comparing. Like this:

'HellO'.downcase.include?('hello')
# true

CodePudding user response:

I can think of two approaches:

  1. downcase before comparing

    str = "HelLO"
    
    str.downcase.include?('hello')
    #=> true
    
  2. use case-insensitive regular rexpression

    str = "HelLO"
    
    str.match?(/hello/i)
    #=> true
    
  • Related