What's the best way to search for a string in a file?

前端 未结 5 2091
悲&欢浪女
悲&欢浪女 2020-12-13 09:07

The title speaks for itself really. I only want to know if it exists, not where it is. Is there a one liner to achieve this?

5条回答
  •  萌比男神i
    2020-12-13 09:33

    Well it seems eed3si9n has the one liner down, here's the longer solution:

    f = File.new("file.txt")
    text = f.read
    if text =~ /string/ then
    #relevant code
    end
    

提交回复
热议问题