You can use string.indexOf('a').

If the char a is present in string :

it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

Answer from mP. on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - Allocates a new String that contains characters from a subarray of the Unicode code point array argument. The offset argument is the index of the first code point of the subarray and the count argument specifies the length of the subarray.
🌐
W3Schools
w3schools.com › java › ref_string_contains.asp
Java String contains() Method
The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not. ... The CharSequence interface is a readable sequence of char values, found in the java.lang package.
🌐
Baeldung
baeldung.com › home › java › java string › java string.contains()
Java String.contains() | Baeldung
April 11, 2025 - A quick example and explanation of the contains API of the standard String class in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-contains-method-example
Java String contains() Method with Example - GeeksforGeeks
July 11, 2025 - In this example, we check if a specific substring is present in the given string. ... // Java Program to demonstrate working contains() method class Gfg { public static void main(String args[]) { String s = "My name is GFG"; System.out.println(s.contains("GFG")); System.out.println(s.contains("geeks")); } }
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-contains-method
Java String contains() method
June 10, 2024 - Java String contains() method checks whether a particular sequence of characters is part of a given string or not.
🌐
TechVidvan
techvidvan.com › tutorials › java-string-contains-method
Java String contains() Method - TechVidvan
March 4, 2024 - contains() method is a built-in Java method that helps us to check whether a sequence of characters is present in a given string or not.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › checking-if-a-string-contains-a-word-or-phrase-in-java-08ef51495d87
Checking if a String Contains a Word or Phrase in Java
June 16, 2025 - Learn how to check if a Java string contains a word or phrase using contains, indexOf, or matches. Covers both case-sensitive and case-insensitive methods.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string contains() method
Java String contains() Method
January 8, 2025 - java.lang.String provides a boolean method contains() that returns ‘true’ if a sequence of characters is found within the given string and ‘false’ otherwise
🌐
DEV Community
dev.to › tpointtechblog › java-string-contains-vs-other-string-methods-what-you-should-know-1hag
Java String contains() vs Other String Methods: What You Should Know - DEV Community
August 31, 2025 - Whether you’re building applications, ... these methods, the java string contains() method is often used to check if a particular sequence of characters exists within a string....
🌐
Codekru
codekru.com › home › java string contains() method
Java String contains() method - Codekru
June 12, 2022 - Method declaration – public boolean contains(CharSequence s). What does it do? – It accepts a sequence of characters in the function’s argument and checks whether that sequence of characters is present in the string or not.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_contains.htm
Java - String contains() Method
The Java String contains() method is used to check whether the current string contains the specified sequence. It returns the Boolean value, which is true if and only if the string contains the sequence of char values; false otherwise.
Top answer
1 of 16
270

EDIT: Here is an update using the Java 8 Streaming API. So much cleaner. Can still be combined with regular expressions too.

public static boolean stringContainsItemFromList(String inputStr, String[] items) {
    return Arrays.stream(items).anyMatch(inputStr::contains);
}

Also, if we change the input type to a List instead of an array we can use items.stream().anyMatch(inputStr::contains).

You can also use .filter(inputStr::contains).findAny() if you wish to return the matching string.

Important: the above code can be done using parallelStream() but most of the time this will actually hinder performance. See this question for more details on parallel streaming.


Original slightly dated answer:

Here is a (VERY BASIC) static method. Note that it is case sensitive on the comparison strings. A primitive way to make it case insensitive would be to call toLowerCase() or toUpperCase() on both the input and test strings.

If you need to do anything more complicated than this, I would recommend looking at the Pattern and Matcher classes and learning how to do some regular expressions. Once you understand those, you can use those classes or the String.matches() helper method.

public static boolean stringContainsItemFromList(String inputStr, String[] items)
{
    for(int i =0; i < items.length; i++)
    {
        if(inputStr.contains(items[i]))
        {
            return true;
        }
    }
    return false;
}
2 of 16
61
import org.apache.commons.lang.StringUtils;

String Utils

Use:

StringUtils.indexOfAny(inputString, new String[]{item1, item2, item3})

It will return the index of the string found or -1 if none is found.

🌐
Team Treehouse
teamtreehouse.com › community › java-string-contains-method
Java String contains method (Example) | Treehouse Community
March 2, 2017 - Frick OFF....lil bih! \n\n"); } } while(isInvalidNoun); String adverb; boolean isInvalidAdverb; do { adverb = console.readLIne("Enter an adverb: "); isInvalidAdverb = (adverb.contains("bitCh")); if(isInvalidAdverb){ console.printf("That language is not allowed.