Java if string has 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

Источник

how to check if a string contains a character in java

how to check if a string contains a character in java?

To check if a string contains a specific character in Java, the following ways can be used-

The java String’s contains() method checks for a particular sequence of characters present within a string. It returns true if the specified character sequence is present within the string, otherwise, it returns false.

The contains() method is case-sensitive.

public static void main(String[] args)

By using indexOf() method —

While the contains() method returns a boolean value, the indexOf() method returns an int type value which is actually the index of the substring or character within the string.

The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it returns -1.

For example «Hello World».indexOf(«World») will return 6 but «Hello World».indexOf(«world») will return -1.

public static void main(String[] args) < String str = "Chercher Tech"; //remember that indexOf() method is case sensitive System.out.println("The position of Tech is "+str.indexOf("Tech" )); >
The position of Tech is 9

Using charAt() method and a loop —

We will execute a loop throughout the string length to find the match for the particular character-

public static void main(String[] args) < String str = "example"; char[] findChar = ; for (int i = 0; i < str.length(); i++) < char ch = str.charAt(i); for (int j = 0; j < findChar.length; j++) < if (findChar[j] == ch) < System.out.println("The character " +findChar[j] + " is present in " + str); >> > >
The character x is present in example The character a is present in example
Recommended Readings

Источник

How to Check if a String Contains a Character in Java

How to Check if a String Contains a Character in Java

Strings in Java are just a sequence of individual characters.

This means that we can search strings for individual characters and detect if they are present.

In this post, we’ll look at examples of the two best ways to search for a character in a string in Java.

Using the String contains() method

The easiest way to check if a string contains another character is to use the contains() method on the String class.

This method is called on a the string itself and takes in a string as the parameter to look for.

Notice how contains() is case-sensitive.

We got false for the second usage because the cases did not match, even though the words did.

Then we got true for the third usage because the cases matched.

In most cases, this is perfectly reasonable and the expected behavior.

One way you can check for a character in a string while ignoring the case is to just make them both lowercase:

Not the most elegant solution but it works.

Using the indexOf() method

Another way to check if a string contains a character is to use the indexOf() method on the String class.

This method takes in a string as a parameter and returns the index of the first occurrence of that string in the string.

Therefore, if the index is greater than or equal to 0, then the string was found, otherwise it was not found.

Let’s see an example of this:

Keeping in mind what was said before, we know that the first and last checks returned true because the index was greater than or equal to 0.

It was unable to find world because of the case difference, and therefore it returned -1 .

Conclusion

In this post, we looked at the two most straight-forward ways to search for a character in a string in Java.

You can either use the contains() method or the indexOf() method to check for the presence of a character in a string.

Hopefully, this has been helpful to you and thanks for reading!

If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!

Give feedback on this page , tweet at us, or join our Discord !

Источник

Читайте также:  Content models in html
Оцените статью