0

I want to check if string contains in another string but in case insensitive manner.

for example - "Kabir" contains in "Dr.kabir's house.". Now "Kabir" with capital K should find in "Dr.kabir's house." with or without spaces in this sentence.

I tried to use contains. But contains() is case sensitive, I also tried to use equalsIgnoreCase() but its not useful.

       for (int i = 0; i < itemsList.size(); i++) {
        if (matching.contains(itemsList.get(i))) {
            item = itemsList.get(i).trim();
            break;
        }
    }

Also tried this by making string uppercase but it checks for all the letters as Uppercase. I want to check if only initial letter is capital.

   for (int i = 0; i < itemsList.size(); i++) {
        if (matching.contains(itemsList.get(i))) {
            item = itemsList.get(i).trim();
            break;
        }
    }

Can anyone help with this please? Thank you..

EDIT : If I want to split "kabir" from the string how to do it?

Sid
  • 2,792
  • 9
  • 55
  • 111
  • 1
    duplicate of this I think, http://stackoverflow.com/questions/14972299/how-to-use-contains-and-equalsignorecase-in-string – msagala25 Dec 13 '16 at 04:59

3 Answers3

4

Just lowercase both strings and then use contains():

for (int i = 0; i < itemsList.size(); i++) {
    if (matching.toLowerCase().contains(itemsList.get(i).toLowerCase())) {
        item = itemsList.get(i).trim();
        break;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I have tried this. But this checks for either all the lower case letters or upper case letters. but what if one word is uppercase and others are small like "Kabir"? – Sid Dec 13 '16 at 04:59
  • 1
    @Sid My solution, as well as the other answers, convert _both_ strings to lowercase, so the case no longer matters, only the letters. Does this make sense? – Tim Biegeleisen Dec 13 '16 at 05:00
  • as an additional, String is immutable. so It will not change the `matching` String unless you assigned the lowercase value to it. – msagala25 Dec 13 '16 at 05:03
  • how can I split "kabir" from the string? @TimBiegeleisen – Sid Dec 13 '16 at 06:11
  • @Sid You can try using a combination of `String#indexOf` and `String#substring` to isolate the string around the substring `kabir`. – Tim Biegeleisen Dec 13 '16 at 06:25
3

Just convert both strings to either lower or upper case first before using .contains() method.

For example:

if (str1.toLowerCase().contains(str2.toLowerCase()))
    //do whatever
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • I have tried this. But this checks for either all the lower case letters or upper case letters. but what if one word is uppercase and others are small like "Kabir"? – Sid Dec 13 '16 at 05:00
  • @Sid It will still work, because you convert both strings before performing the match. So neither strings will contain any upper case letter before the check. – user3437460 Dec 13 '16 at 05:01
2

make both strings lower (or upper) case

String one = "test";
String two = "TESTY";

if (two.toLowerCase ().contains (one.toLowerCase ())) {
    System.out.println ("Yep");
}
else {
    System.out.println ("Nope");
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I have tried this. But this checks for either all the lower case letters or upper case letters. but what if one word is uppercase and others are small like "Kabir"? – Sid Dec 13 '16 at 05:00
  • this is making both words lowercase – Scary Wombat Dec 13 '16 at 05:00