Check if a String Contains Character in Java

Payel Ganguly Dec 21, 2022 Oct 11, 2020
  1. Use String contains() Method to Check if a String Contains Character
  2. Use String indexOf() Method to Check if a String Contains Character
  3. Use String contains() Along With if-else Statement
  4. Java Program to Search for Certain Characters Present in a String
Check if a String Contains Character in Java

This tutorial article will introduce how to check if a string contains a specific character in Java. In Java, we use the contains() method in different ways to check the presence of characters in a string. Let us discuss this method implementation through various examples.

Use String contains() Method to Check if a String Contains Character

Java String’s contains() method checks for a particular sequence of characters present within a string. This method returns true if the specified character sequence is present within the string, otherwise, it returns false. Let us follow the below example.

import java.util.*;
import java.lang.*;
import java.io.*;

public class Example1 {
    public static void main(String[] args) {
        String str = "Character";
        System.out.println(str.contains("h"));
        System.out.println(str.contains("Char"));
        System.out.println(str.contains("ac"));
        System.out.println(str.contains("v"));
        System.out.println(str.contains("vl")); 
    }
}

Output:

true
true
true
false
false

Please note, the contains() method is case-sensitive. If we try looking for CHA in our given string, then the result will be false, like below,

import java.util.*;
import java.lang.*;
import java.io.*;

public class Example {
    public static void main(String[] args) {
        String str = "Character";
        System.out.println(str.contains("H"));
        System.out.println(str.contains("CHAR"));
        System.out.println(str.contains("aCt")); 
    }
}  

Output:

false
false
false  

Use String indexOf() Method to Check if a String Contains Character

In this example, we will learn to find the character within a string using the indexOf() method. The indexOf() method is different from the contains() method as it does not return any Boolean value. Instead, indexOf() returns an int value which is actually the index of the substring within the string. Let us understand the below example.

import java.util.*;
import java.lang.*;
import java.io.*;

public class Example2 {
    public static void main(String[] args) {
        String str = "Hello World!";
        if(str.indexOf("World") != -1)
        {
            System.out.println("The String "+str+" contains World");      
        }
        else
        {
            System.out.println("The String "+str+"does not contain World"); 
        } 
    }
}  

Output:

The string Hello World! contains World

Use String contains() Along With if-else Statement

According to the character presence, we are now aware that the Java string contains() method returns a Boolean value. For this, we can use this method in an if-else conditional statement. Let us discuss in the below example.

import java.util.*;
import java.lang.*;
import java.io.*;

public class Example3 {
    public static void main(String[] args) {
        String str = "Hello World!";
        if(str.contains("World")) 
        {
            System.out.println("It is true");
        }
        else
        {
            System.out.println("It is false");  
        }
    }
} 

Output:

It is true

Java Program to Search for Certain Characters Present in a String

This last example will make a generic Java program to search certain characters present within a string or not. In that case, we will execute a loop throughout the string length to find the match for the character set. Let us check the below example.

import java.util.*;
import java.lang.*;
import java.io.*;

public class Example4 {
    public static void main(String[] args) {
        String str = "yellow";
        char[] charSearch = {'y','e','w'}; 
        for(int i=0; i<str.length(); i++) 
        {
            char chr = str.charAt(i);
            for(int j=0; j<charSearch.length; j++)
            {
                if(charSearch[j] == chr)
                {
                    System.out.println("Char Value "+charSearch[j]+" is present in "+str);      
                }
            }  
        }
    }
} 

Output:

Char Value y is present in yellow
Char Value e is present in yellow
Char Value w is present in yellow

Related Article - Java String