Serhii Danovskyi
Serhii Danovskyi

Reputation: 394

String include string ruby

I have a string like this

string = 'Company Bla potato text 123 random foo bar'

How can I detect including string in string?

string.include?('potato') => true
string.include?('Company Bla') => true

this work fine but how I can detect these cases:

string.include?('Potato') => false
string.include?('Company BLA') => false
string.include?('RANDOM') => false

they return false but I need true.

Upvotes: 0

Views: 326

Answers (2)

steenslag
steenslag

Reputation: 80065

s = "Company BLA"
p string.match?(/#{s}/i)

The trailing i tells the regex to ignore case.

Upvotes: 3

Ursus
Ursus

Reputation: 30056

One simple way is to make both downcase or uppercase

s = 'Potato'
string.downcase.include?(s.downcase)

Upvotes: 6

Related Questions