How can I get Browser.text.include? to be case insensitive?
Asked Answered
S

2

8

It's as simple as that:

How can I get Browser.text.include?, or Ruby in general, to be case insensitive for that specified command?

Serpigo answered 28/7, 2011 at 15:51 Comment(0)
D
11

One of the easiest ways is to downcase or upcase the text that you're reading:

Browser.text.downcase.include?

Then, you need to make sure that your desired text is supplied in all lowercase.

Defer answered 28/7, 2011 at 16:53 Comment(2)
Maybe you downcase both strings?Hereunder
@Dave - yes, just depends on how much control you have over the incoming data. Using downcase on both would be the safest way.Defer
D
5

You can use String#match with a regular expression. e.g.:

("CaseSensitive".match /SENSITIVE/i) != nil

That will return true if there is a case-insensitive match, false otherwise. So for the above example, it returns true, as 'SENSITIVE' is found within 'CaseSensitive'.

For your example:

(Browser.text.match /yourString/i ) != nil
Dejected answered 28/7, 2011 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.