. Advertisement .
..3..
. Advertisement .
..4..
There are multiple built-in methods of the String object that can help you check if a string contains character in Java. Check them out below.
Check If A String Contains Character In Java
With contains()
The method contains()
of the String object searches for a sequence of characters in a string. It returns true if the sequence does exist in the string and false otherwise.
Example:
public class MyClass {
public static void main(String args[]) {
String str1 = "This is ITTutoria.net";
System.out.println(str1.contains("ITTutoria"));
System.out.println(str1.contains("Quora"));
}
}
Output:
true
false
The first println() statement prints true because there is “ITTutoria” in the string str1, while the second one returns false because there is no “Quora” in it. (Read this guide if you want to learn more about the println() function).
Remember that contains() takes letter cases into account when it does the comparison, meaning it only returns true when not only the characters but also their cases must match the search pattern.
Example:
public class MyClass {
public static void main(String args[]) {
String str1 = "This is ITTutoria.net";
System.out.println(str1.contains("ITTutoria"));
System.out.println(str1.contains("ittutoria"));
}
}
Output:
true
false
As you can see, the second statement returns false because the sequence and the actual characters in the string don’t have the same cases. If you want to ignore cases when searching for a character set, you can use the simplest solution – the method toLowerCase(). It creates a lower-case version of the original string, which then can be used for our comparison. This is how we can rewrite the first example:
public class MyClass {
public static void main(String args[]) {
String str1 = "This is ITTutoria.net";
System.out.println(str1.toLowerCase().contains("ittutoria"));
System.out.println(str1.toLowerCase().contains("quora"));
}
}
Output:
true
false
As expected, the first statement prints true, even if we write the search pattern in lower cases.
indexOf()
The indexOf()
prints the index of the first occurrence of a character or substring within the given string. If there is no such substring, the method returns -1. This way, you can’t only know whether there is a substring in a longer string but also know its location.
This is how you can replace contains()
with indexOf()
:
public class MyClass {
public static void main(String args[]) {
String str1 = "This is ITTutoria.net";
System.out.println(str1.indexOf("ITTutoria"));
System.out.println(str1.indexOf("Quora"));
}
}
Output:
8
-1
The second statement returns -1 because there is no “Quora” in “This is ITTutoria.net”. But since the first search pattern has its match, the method indexOf()
returns the index of the first character “I”. Remember that white space is counted too, and strings are zero-indexed.
Keep in mind that this method has an optional parameter that allows specifying the search region within the original string:
public int indexOf(String str, int fromIndex)
If fromIndex is set, the method indexOf() will perform the search for the substring from that location. It can come in handy when a substring may have several occurrences, but you only want to find it after a specific location.
Example:
public class MyClass {
public static void main(String args[]) {
String str1 = "You can use for loops for different purposes.";
System.out.println(str1.indexOf("for"));
System.out.println(str1.indexOf("for", 15));
}
}
Output:
12
22
There are “for” words in the string. If you limit the search with the fromIndex parameter, it will print the index of the latter word, not the first.
lastIndexOf()
The method lastIndexOf()
works in the opposite direction as indexOf(). It will find the latest occurrence of a substring, not first.
Example:
public class MyClass {
public static void main(String args[]) {
String str1 = "You can use for loops for different purposes.";
System.out.println(str1.indexOf("for"));
System.out.println(str1.lastIndexOf("for"));
}
}
Output:
12
22
You can see how the two methods return two different values because they search in different directions.
Conclusion
You can check if a string contains character in Java with contains()
, indexOf()
, or lastIndexOf()
. While they all are methods of the String class, keep in mind that they return different things.
Leave a comment