String

Java String Contains Example

Photo of Nikos MaravitsasNikos MaravitsasMarch 12th, 2014Last Updated: March 12th, 2014
0 117 1 minute read

In this example we are going to see how you can check if aString contains anotherString in Java. The Java String Class API offers a method to do that,contains. To be more specific the complete signature ofcontains method ispublic boolean contains(CharSequence s). Which means that you are trying to see, whether aCharSequences resides inside a String. ACharSequence is an interface that denotes a readable sequence ofchars. It is used to provide uniformly read-only access to different kinds ofchar sequences. As you’ve probably seen in the documentation,String class implementsCharSequence interface, among others likeSerializable andComparable<String>.

Let’s see how you can use it :

StringContainsExample.java

package com.javacodegeeks.core.string;public class StringContainsExample {public static void main(String[] args){System.out.println("abcdefghiklmpo".contains("cde"));}}

The above program will check if the string"abcdefghiklmpo" contains the string"cde". It will output:

true

As you might imagine, you can use contains with all other classes that implementCharSequence interface. An example isStringBuilder.

Let’s see how you can use it:

StringContainsExample.java

package com.javacodegeeks.core.string;public class StringContainsExample {public static void main(String[] args){StringBuilder sBuilder = new StringBuilder();sBuilder.append("Java").append(" ").append("Code").append(" ").append("Geeks");System.out.println("Java Code Geeks are awsome".contains(sBuilder));}}

This will output:

true

This was a Java String Contains Example.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Nikos MaravitsasNikos MaravitsasMarch 12th, 2014Last Updated: March 12th, 2014
0 117 1 minute read
Photo of Nikos Maravitsas

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.