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:
JAVApublic 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"));
}
}
BASHtrue
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:
JAVApublic 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()));
}
}
BASHtrue
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:
JAVApublic 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"));
}
}
BASH1
-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!
- Managing PHP Dependencies with Composer
- Create an RSS Reader in Node
- How to deploy a .NET app using Docker
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- How to deploy an Express app using Docker
- Getting Started with Sass
- Getting Started with Handlebars.js
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Learn how to build a Slack Bot using Node.js