StringUtils containsAny() Method

StringUtils containsAny() Method | In this blog, we will discuss the containsAny() method of StringUtils class. The containsAny() method is used to check whether the given string is present in the substring or not. The contains() method of StringUtils class also does the same task but the difference is in the contains() method searches for the same pattern in the main string but for the containsAny() method the string need not be the same as that of the main string, it can be similar.

The containsAny() method returns the boolean value. If the values are the same then it returns true or if the values are not the same then it returns false. Note that containsAny() method is case-sensitive.

StringUtils.containsAny() Method

There are three overloaded forms of the StringUtils containsAny() method:-

  1. containsAny(CharSequence cs, char… searchChars)
  2. containsAny(CharSequence cs, CharSequence… searchCharSequences)
  3. containsAny(CharSequence cs, CharSequence searchChars)

The containsAny(CharSequence cs, char… searchChars) and containsAny(CharSequence cs, CharSequence… searchCharSequences) method checks if the CharSequence contains any character in the given set of characters. The containsAny(CharSequence cs, CharSequence searchChars) method checks if the CharSequence contains any of the CharSequences in the given array.

For all these overloaded forms of containsAny() methods, a null CharSequence will return false. A null CharSequence will return false whereas a null or zero-length search array will return false.

The example for the containsAny() method is as follows:-
Example1:
String: “Apple”
String1: “pp”
The String contains String1: true.
Example2:
String2: “oooo”
The String does not contain any of String2: false.

StringUtils containsAny() Method Example

Let us see an example for StringUtils containsAny(CharSequence cs, CharSequence searchChars) method through the program.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(StringUtils.containsAny("Hello", "Hello"));
        System.out.println(StringUtils.containsAny("Hello", "H"));
        System.out.println(StringUtils.containsAny("Hello", "Who"));
        System.out.println(StringUtils.containsAny("Hello", "Sir"));
        System.out.println(StringUtils.containsAny("Hello", "Shift"));
    }
}

Output:-

true
true
true
false
false

In the String “Hello” there are four unique characters:- ‘H’, ‘e’, ‘l’, ‘o’. In the first line, we are checking whether the given string contains any character of the string “Hello” and since both are the same string therefore it returns true.

In the second line, we are checking whether the string contains the ‘H’ character or not and it returns true. In the third line, we are checking whether the string contains any characters from the string literal “Who” and since ‘o’ is common among them therefore it returns true. Note that containsAny() method is case sensitive so ‘H’ from “Hello” and ‘h’ from “Who” is not the same.

In the fourth line, we are checking whether the string contains any character of the string literal “Sir”, but the “Hello” string doesn’t contain ‘S’, ‘i’, or ‘r’ characters therefore it returns false. Similarly in the fifth line, it returns false.

The above program can be written as follows using the StringUtils containsAny(CharSequence cs, char… searchChars) method:-

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(StringUtils.containsAny("Hello", 'H', 'e', 'l', 'l', 'o'));
        System.out.println(StringUtils.containsAny("Hello", 'H'));
        System.out.println(StringUtils.containsAny("Hello", 'W', 'h', 'o'));
        System.out.println(StringUtils.containsAny("Hello", 'S', 'i', 'r'));
        System.out.println(StringUtils.containsAny("Hello", 'S', 'h', 'i', 'f', 't'));
    }
}

Output:-

true
true
true
false
false

The above program can be written as follows using the StringUtils containsAny(CharSequence cs, CharSequence… searchCharSequences) method:-

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(StringUtils.containsAny("Hello", "H", "e", "l", "l", "o"));
        System.out.println(StringUtils.containsAny("Hello", "H"));
        System.out.println(StringUtils.containsAny("Hello", "W", "h", "o"));
        System.out.println(StringUtils.containsAny("Hello", "S", "i", "r"));
        System.out.println(StringUtils.containsAny("Hello", "S", "h", "i", "f", "t"));
    }
}

Output:-

true
true
true
false
false

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *