Java String Handling

Java String Contains Example

Java String Contains Example shows how to check if String contains another String using contains method. Example also shows whether contains is case sensitive or not.

How to check if String contains another String in Java?

To check if String contains another String, you can use contains method of String class.

This method returns true if the String contains specified character sequence.

Java String contains example

Output

Note: Contains method does not accept regular expression as an argument. If you want to use regular expression, you can use matches method of String class.

If you are using Apache Commons library, you can use contains method of StringUtils class to check if String contains another String as given below.

Output

Is Java String contains case sensitive?

Yes, String contains method is case sensitive. Let’s have a look at the below code snippet.

Output

In order to do case insensitive contains search or ignore case, you need to make both the Strings to either upper case or lower case before invoking the method as given below.

Output

If you are using Apache Commons library, you can use containsIgnoreCase method of StringUtils class to make case insensitive contains comparison or to ignore case as given below.

Output

Note: If you want to check if string contains a number, please see check if String contains number example.

Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *