Rathishkumar
Rathishkumar

Reputation: 119

Check whether the string contains the given string or not

For ex: a.text has name "capybara" and I need to check whether the string has characters "ba" in it.

if a.text.contains?("ba")
  # do something
else
  # do something
end

Upvotes: 1

Views: 3082

Answers (2)

mswieboda
mswieboda

Reputation: 1026

If you want more flexibility with regex, use match like:

if a.text.match(/ba/)
  # do something
end

Upvotes: 2

Anthony
Anthony

Reputation: 15967

text = "capybara"
text.include?("ba")
=> true

so you could do:

if text.include?("ba")
  # do something
else
  # do something else
end

Upvotes: 6

Related Questions