How to check whether a string contains a substring in Ruby

后端 未结 9 973
南旧
南旧 2020-11-28 18:02

I have a string variable with content:

varMessage =   
            \"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\\n\"


            \"/my/name/is/balaji.so\\n\"
                 


        
9条回答
  •  温柔的废话
    2020-11-28 18:07

    A more succinct idiom than the accepted answer above that's available in Rails (from 3.1.0 and above) is .in?:

    my_string = "abcdefg"
    if "cde".in? my_string
      puts "'cde' is in the String."
      puts "i.e. String includes 'cde'"
    end
    

    I also think it's more readable.

    See the in? documentation for more information.

    Note again that it's only available in Rails, and not pure Ruby.

提交回复
热议问题