In Java, How to Check If a String Contains a Substring (Ignoring Case)

In Java, how do I check if a string contains a substring (ignoring case)?

str1.toUpperCase().contains(str2.toUpperCase())

UPD:

Original answer was using toLowerCase() method. But as some people correctly noticed, there are some exceptions in Unicode and it's better to use toUpperCase(). Because:

There are languages knowing more than one lower case variant for one upper case variant.

How to check if a String contains another String in a case insensitive manner in Java?

Yes, contains is case sensitive. You can use java.util.regex.Pattern with the CASE_INSENSITIVE flag for case insensitive matching:

Pattern.compile(Pattern.quote(wantedStr), Pattern.CASE_INSENSITIVE).matcher(source).find();

EDIT: If s2 contains regex special characters (of which there are many) it's important to quote it first. I've corrected my answer since it is the first one people will see, but vote up Matt Quail's since he pointed this out.

String contains - ignore case

You can use

org.apache.commons.lang3.StringUtils.containsIgnoreCase(CharSequence str,
CharSequence searchStr);

Checks if CharSequence contains a search CharSequence irrespective of
case, handling null. Case-insensitivity is defined as by
String.equalsIgnoreCase(String).

A null CharSequence will return false.

This one will be better than regex as regex is always expensive in terms of performance.

For official doc, refer to : StringUtils.containsIgnoreCase

Update :

If you are among the ones who

  • don't want to use Apache commons library
  • don't want to go with the expensive regex/Pattern based solutions,
  • don't want to create additional string object by using toLowerCase,

you can implement your own custom containsIgnoreCase using java.lang.String.regionMatches

public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)

ignoreCase : if true, ignores case when comparing characters.

public static boolean containsIgnoreCase(String str, String searchStr)     {
if(str == null || searchStr == null) return false;

final int length = searchStr.length();
if (length == 0)
return true;

for (int i = str.length() - length; i >= 0; i--) {
if (str.regionMatches(true, i, searchStr, 0, length))
return true;
}
return false;
}

Ignore case for 'contains' for a string in Java

You need to convert both the strings to the same case before using contains

s.toLowerCase().contains("ABCD".toLowerCase());

Can we check if string contains in another string with case insensitive?

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;
}
}

Option to ignore case with .contains method?

I'm guessing you mean ignoring case when searching in a string?

I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.

// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);

Edit:
As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.

How to check if a string contains a substring with ignoring case in Angular

Convert the string to lower case and then you can check if it is present in the url:

let show = this.router.url.toLocaleLowerCase().contains('gclid');

Check If a substring is in a string ignoring uppercase, lowercase and special characters?

Use the toLowerCase method of String


word = word.replaceAll("[^a-zA-Z]","").toLowerCase(); // keep only letters

Per Andreas suggestion, convert word to lower case before looping. It is more efficient.

for (int i=0; i < recipesFounded.size(); i++) {
if (recipesFounded.get(i).getTitle().toLowerCase()
.contains(word)) {
trueOnes.add(recipesFounded.get(i));
}
}

Since List implements the iterable interface, you can do it like this. It presumes that you are using a class called Recipe

for (Recipe recipe : recipesFounded) {
if (recipe.getTitle().toLowerCase()
.contains(word)) {
trueOnes.add(recipe);
}
}

Case-insensitive substring search

The easiest way is to just use toLowerCase() or toUpperCase() on both strings.

Check if a Java Set contains a particular string, independent of case

When constructing the set, use a sorted set with a case insensitive comparator. For example:

Set<String> s = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
s.addAll(Arrays.asList("one", "two", "three"));

//Returns true
System.out.println("Result: " + s.contains("One"));


Related Topics



Leave a reply



Submit