Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine whether a string contains any of a set of strings

Tags:

string

ruby

I'm using Mechanize to interact with a few web pages, and I'm trying to determine whether a given form submission resulted in an error.

Right now I'm doing this:

agent.page.body.include?("I'm an error message!")

But I just discovered another error message. Since I don't want to do:

agent.page.body.include?("I'm an error message!") || agent.page.body.include?("Another error message")

How can I determine whether the page body contains either error message?

like image 669
Tom Lehman Avatar asked Jan 28 '11 21:01

Tom Lehman


People also ask

How do you check if a string contains any of some strings?

Using String.contains() method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.

How do I check if a string contains any value?

The String. indexOf method returns the index of the first occurrence of a substring in a string, or -1 if the substring is not contained in the string. If the indexOf method doesn't return -1 , the substring is contained in the string.

How do I check if a string contains an array of strings?

includes() You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if a string contains a set of characters in Python?

Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .


1 Answers

error_messages.any? { |mes| agent.page.body.include? mes }
like image 64
Nakilon Avatar answered Oct 20 '22 15:10

Nakilon