This writeup will help you to learn the method to check if a String contains a character in Java.
How to Check if a String Contains a Character in Java?
For checking if a String contains a character in Java, there are different methods listed below:
- Using contains() method
- Using indexOf() method
- Using for loop
Let’s go through each of these approaches individually.
Method 1: Check if a String Contains Character in Java Using contains() Method
To determine whether a given string contains a particular string of characters or not, use the “contains()” method. Here, the sequence of characters refers to the collection of characters. This method accepts a set of characters as a parameter, searches that in the String, and returns a boolean value “true” if the String contains the specified sequence of characters; otherwise, it returns “false”.
Syntax
Follow the below syntax for using contains() method:
This method takes a sequence of characters “char” as a parameter which represents a substring going to be searched in the “str” String.
Example
In this example, we have a String named “str” initialized with “LinuxHint”:
Now, we will call the “contains()” method with the String “str” and pass the set of characters “nux” to check whether this substring exists in the String. Remember that the “contains()” method is case-sensitive:
The output displays “true”, which indicates “nux” is a part of the specified characters:
Note: The character is enclosed in single quotes (‘ ‘), while a String or a set of characters is surrounded by double quotes (” “).
Let’s move to the method that will check for a single character in a String.
Method 2: Check if a String Contains Character in Java Using indexOf() Method
To find a single character in a String, the “indexOf()” method is used. It returns an int value representing the character’s index in the String.
The “contains()” method only determines whether a String is present or absent in the String. It cannot find out the index where the searched substring is. Because of these constraints, it is preferable to utilize the “indexOf()” method rather than the “contains()” method.
Syntax
The syntax for the “indexOf()” method is described below:
It takes a character “char” as a parameter and outputs the index of the character in the String “str”.
Example
Here, we will check if “str” contains the “H” character by passing it as an argument to the “indexOf()” method:
The output shows that the character “H” is present at the “5” index of the “LinuxHint” String:
Want to check the different characters present in a String? Follow the next section.
Method 3: Check if a String Contains Character in Java Using for Loop
In this section, we will determine whether a String contains particular characters or not. In this scenario, a loop will be executed according to the length of the String to find out the matched character set.
Syntax
For checking the particular characters in a String, follow the below syntax of the “for” loop:
The loop will start from “0” and iterate till the String’s length.
Example
We will use the same String “LinuxHint” and create an array of characters to be searched in the String named “charToSearch”:
Add two “for” loops, one will iterate through the String, and the second one will iterate the character array. An “if” statement is also added to match the String with the character of the array:
{
char ch = str.charAt(i);
for(int j=0; j<charToSearch.length; j++)
{
if(charToSearch[j] == ch)
{
System.out.println("The String "+str + " contains the char " +charToSearch[j]);
}
}
}
The output signifies that all of the specified characters are present in the “LinuxHint” String:
We assembled all the necessary instructions to check whether the String contains characters in Java.
Conclusion
To check if a String contains a character in Java, there are different methods: contains() method, indexOf() method, and the for Loop. The best method to check the character in a String is the indexOf() method. It will return the index of the character present in the String, while contains() method only returns a boolean value indicating the presence or absence of the specified characters. In this write-up, we explained the methods to check a character in a String.