How to Check If a String Contains a Character in JAVA

You are currently viewing How to Check If a String Contains a Character in JAVA

In this tutorial, I am going to teach “How to check if a string contains a character in JAVA?”. In JAVA, you can use contains() method and different methods to check a string contains a character or not. Okay, let’s move on to the tutorial.

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

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
false
true
false
false

Do you know? contains() method is case sensitive in JAVA. In the following code, the string is “Character” but we are going to check “CHAR” is available on the string. The code will return false because contains() method is case sensitive.

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

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

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

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);      
                }
            }  
        }
    }
} 
Char Value y is present in yellow
Char Value e is present in yellow
Char Value w is present in yellow

Final Words

I hope this article helps you to Check If a String Contains a Character in JAVA. If you face any issues please let me know via the comment section. Share this article with other JAVA developers via social networks.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply