


indexOf() method is a bit more crude than the. (string.toUpperCase().contains(substring.toUpperCase())) Oftentimes, to avoid this issue, since we're not looking for case sensitivity, you'd match the case of both Strings before checking: (string.toLowerCase().contains(substring.toLowerCase())) If we tried looking for "Va" in our string, the result would be false. The method accepts a CharSequence and returns true if the sequence is present in the String we call the method on: String string = "Java" It's provided by the String class itself and is very efficient. The first and foremost way to check for the presence of a substring is the. One approach that is unique to Java, however, is the use of a Pattern class, which we'll cover later in the article.Īlternatively, you can use Apache Commons and the helper class StringUtils, which offers many derived methods from the core methods for this purpose. There's a couple of ways to do this in Java, and most of them are what you'd expect to see in other programming languages as well. Other times, we wish to alter the flow if a String contains (or lacks) a certain substring, which could be a command. For example, sometimes we wish to break a String if it contains a delimiter at a point. Checking for substrings within a String is a fairly common task in programming.
