How to check whether a string contains a substring in Ruby

后端 未结 9 969
南旧
南旧 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:20

    You can also do this...

    my_string = "Hello world"
    
    if my_string["Hello"]
      puts 'It has "Hello"'
    else
      puts 'No "Hello" found'
    end
    
    # => 'It has "Hello"'
    

    This example uses Ruby's String #[] method.

提交回复
热议问题