How to Check if a String Contains a Character in Java

Updated onbyAlan Morel
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.

Let's look at an example:

JAVA
public class Main { public static void main(String[] args) { String str = "Hello World"; System.out.println(str.contains("e")); System.out.println(str.contains("world")); System.out.println(str.contains("World")); } }
BASH
true false true

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:

JAVA
public class Main { public static void main(String[] args) { String str = "Hello World"; System.out.println(str.toLowerCase().contains("e".toLowerCase())); System.out.println(str.toLowerCase().contains("world".toLowerCase())); System.out.println(str.toLowerCase().contains("World".toLowerCase())); } }
BASH
true true true

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:

JAVA
public class Main { public static void main(String[] args) { String str = "Hello World"; System.out.println(str.indexOf("e")); System.out.println(str.indexOf("world")); System.out.println(str.indexOf("World")); } }
BASH
1 -1 6

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!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.