0

I'm trying to make it ignore the cases, while using the contain method. How do I do that?

String text = "Did you eat yet?";   

if(text.contains("eat") && text.contains("yet"))
    System.out.println("Yes");
else
    System.out.println("No.");
msrd0
  • 7,816
  • 9
  • 47
  • 82
nmelssx
  • 100
  • 1
  • 12

2 Answers2

1

Please use

 org.apache.commons.lang3.StringUtils.containsIgnoreCase("ABCDEFGHIJKLMNOP", "gHi");
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
1

Unfortunately there's no String.containsIgnoreCase method of String.

However, you can verify a similar condition with regular expressions.

For instance:

String text = "Did you eat yet?";
// will match a String containing both words "eat", 
// then "yet" in that order of appearance, case-insensitive
//                           | word boundary
//                           |        | any character, zero or more times 
Pattern p = Pattern.compile("\\beat\\b.*\\byet\\b", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
System.out.println(m.find());

Simpler version (thanks blgt):

// here we match the whole String, so we need start-of-input and 
// end-of-input delimiters
//                               | case-insensitive flag
//                               |   | beginning of input
//                               |   |                        | end of input
System.out.println(text.matches("(?i)^.*\\beat\\b.*\\byet\\b.*$"));

Output

true
Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • You can strip out the boilerplate `Pattern` by using `String.matches(regex)` with a `?i` flag, [info here](https://blogs.oracle.com/xuemingshen/entry/case_insensitive_matching_in_java) – blgt Aug 29 '14 at 15:40
  • @blgt thanks, let me improve this. – Mena Aug 29 '14 at 15:41
  • 1
    Where do I put, "eat", and "yet"? – nmelssx Aug 29 '14 at 16:40
  • 1
    I think this answer is missing a bunch of everything. When I tried to put it in Eclipse all it did was shown everything as an error. I'm new to java so I don't know what you're missing if you're not going to put it in the code. – nmelssx Aug 29 '14 at 16:59
  • @nmelssx this ansewr requires you to import the necessary dependencies, all in Java SE. In Eclipse you can simply to that by pressing Ctrl-Shift-O (assuming Windows). Otherwise, a little research on the API and packages for `Pattern` and `Matcher` should help you. The "simpler version" doesn't even require any additional import. Both are tested and working within the scope of your question. – Mena Aug 29 '14 at 17:07
  • @Mena but in the code it doesn't even say do that if it finds both the word "eat", and "yet"? – nmelssx Aug 29 '14 at 17:11
  • ?i)^.*\\beat\\b.*\\byet\\b.*$" And what about this part? Can you explain it please? – nmelssx Aug 29 '14 at 17:17
  • @nmelssx of course it does, that's the purpose. Will comment a bit on the `Pattern`s but you should do some research on your own. – Mena Aug 29 '14 at 17:23
  • "(?i)^"eat,yet".*\\beat\\b.*\\byet\\b.*$" So just put eat and yet where it begins like this? – nmelssx Aug 29 '14 at 17:27
  • Okay. I'm an idiot for not seeing that it said "beat", and "byet", but you should've pointed that out. – nmelssx Aug 29 '14 at 17:55
  • Thank you. I got it now. – nmelssx Aug 29 '14 at 17:56