Java contains any character

Check if a String Contains Character in Java

Check if a String Contains Character in Java

  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

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"));  > > 
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"));  > > 

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");  >  > > 
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");  >  > > 

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; istr.length(); i++)    char chr = str.charAt(i);  for(int j=0; jcharSearch.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 

Related Article — Java String

Источник

Check if a String Contains Character in Java

Check if a String Contains Character in Java

  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

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"));  > > 
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"));  > > 

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");  >  > > 
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");  >  > > 

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; istr.length(); i++)    char chr = str.charAt(i);  for(int j=0; jcharSearch.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 

Related Article — Java String

Источник

How to Check if a String Contains Character in Java

The Java wrapper class “Character” wraps a primitive data type “char“. Java utilizes the “char” data type to hold a single character enclosed in single quotes (‘ ‘), as opposed to the “String” data type that belongs to the Java wrapper class “String”, used to carry a sequence of characters enclosed in double quotations (” “). There are times while writing Java code that you need to verify if a certain character or a group of characters are present in the sequence or not.

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:

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.

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:

for ( int i = 0 ; i < str. length ( ) ; i ++ )
{
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.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Читайте также:  Python convert minutes to hours and minutes
Оцените статью