Check if a String contains another String in Java
Core Java classes provide different solutions that developers can use to check if a String contains another String inside it. In this article we will show you different Java code examples how to check if a String is inside another String using core Java methods such as String.contains(), String.indexOf(), String.lastIndexOf(), String.matches() and java.util.regex.Pattern class.
Java code how to use String.contains()
In this code example we use String.contains() which returns a boolean value true if and only if the String contains a specific substring. By default this method is case sensitive.
public class StringContainsExample1 {
public static void main(String[] args) {
String testString = "Java was released in 1995";
String search1 = "Java";
String search2 = "java";
String search3 = "C#";
boolean isContains1 = testString.contains(search1);
boolean isContains2 = testString.contains(search2);
boolean isContains3 = testString.contains(search3);
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
false
false
Java code how to use String.contains() with case insensitive
In this code example we use String.contains() with support case insensitive by converting the Strings to lowercase.
public class StringContainsExample2 {
public static void main(String[] args) {
String testString = "Java was released in 1995";
String search1 = "Java";
String search2 = "java";
String search3 = "C#";
boolean isContains1 = testString.toLowerCase().contains(search1.toLowerCase());
boolean isContains2 = testString.toLowerCase().contains(search2.toLowerCase());
boolean isContains3 = testString.toLowerCase().contains(search3.toLowerCase());
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
true
false
Java code how to use String.indexOf()
In this code example we use String.indexOf() which returns the index number of a specific substring. Based on this index we can figure out whether the String contains substring or not. By default this method is case sensitive.
public class StringIndexOfExample1 {
public static void main(String[] args) {
String testString = "Java was released in 1995";
String search1 = "Java";
String search2 = "java";
String search3 = "C#";
int indexOf1 = testString.indexOf(search1);
boolean isContains1 = indexOf1 >= 0;
int indexOf2 = testString.indexOf(search2);
boolean isContains2 = indexOf2 >= 0;
int indexOf3 = testString.indexOf(search3);
boolean isContains3 = indexOf3 >= 0;
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
false
false
Java code how to use String.indexOf() with case insensitive
In this code example we use String.indexOf() with support case insensitive by converting the Strings to lowercase.
public class StringIndexOfExample2 {
public static void main(String[] args) {
String testString = "Java was released in 1995";
String search1 = "Java";
String search2 = "java";
String search3 = "C#";
int indexOf1 = testString.toLowerCase().indexOf(search1.toLowerCase());
boolean isContains1 = indexOf1 >= 0;
int indexOf2 = testString.toLowerCase().indexOf(search2.toLowerCase());
boolean isContains2 = indexOf2 >= 0;
int indexOf3 = testString.toLowerCase().indexOf(search3.toLowerCase());
boolean isContains3 = indexOf3 >= 0;
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
true
false
Java code how to use String.lastIndexOf()
In this code example we use String.lastIndexOf() which returns the last index number of a specific substring. Based on this index we can figure out whether the String contains substring or not. By default this method is case sensitive.
public class StringLastIndexOfExample1 {
public static void main(String[] args) {
String testString = "Java was released in 1995";
String search1 = "Java";
String search2 = "java";
String search3 = "C#";
int lastIndexOf1 = testString.lastIndexOf(search1);
boolean isContains1 = lastIndexOf1 >= 0;
int lastIndexOf2 = testString.lastIndexOf(search2);
boolean isContains2 = lastIndexOf2 >= 0;
int lastIndexOf3 = testString.lastIndexOf(search3);
boolean isContains3 = lastIndexOf3 >= 0;
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
false
false
Java code how to use String.lastIndexOf() with case insensitive
In this code example we use String.lastIndexOf() with support case insensitive by converting the Strings to lowercase.
public class StringLastIndexOfExample2 {
public static void main(String[] args) {
String testString = "Java was released in 1995";
String search1 = "Java";
String search2 = "java";
String search3 = "C#";
int lastIndexOf1 = testString.toLowerCase().lastIndexOf(search1.toLowerCase());
boolean isContains1 = lastIndexOf1 >= 0;
int lastIndexOf2 = testString.toLowerCase().lastIndexOf(search2.toLowerCase());
boolean isContains2 = lastIndexOf2 >= 0;
int lastIndexOf3 = testString.toLowerCase().lastIndexOf(search3.toLowerCase());
boolean isContains3 = lastIndexOf3 >= 0;
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
true
false
Java code how to use String.matches() with regular expression
In this code example we use String.matches() method with a regular expression to find a substring in a String. Our regex also include (?i) to make the regex case insensitive.
public class StringMatchesExample {
public static void main(String[] args) {
String testString = "Java was released in 1995";
boolean isContains1 = testString.matches("(?i).*Java.*");
boolean isContains2 = testString.matches("(?i).*java.*");
boolean isContains3 = testString.matches("(?i).*C#.*");
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
true
false
Java code how to use java.util.regex.Pattern with regular expression
In this code example we also use a regular expression with java.util.regex.Pattern class.
import java.util.regex.Pattern;
public class StringCheckContainsPattern {
public static void main(String[] args) {
String testString = "Java was released in 1995";
Pattern pattern1 = Pattern.compile("(?i).*Java.*");
boolean isContains1 = pattern1.matcher(testString).matches();
Pattern pattern2 = Pattern.compile("(?i).*java.*");
boolean isContains2 = pattern2.matcher(testString).matches();
Pattern pattern3 = Pattern.compile("(?i).*C#.*");
boolean isContains3 = pattern3.matcher(testString).matches();
System.out.println(isContains1);
System.out.println(isContains2);
System.out.println(isContains3);
}
}
true
true
false
Happy Coding 😊