HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java String contains() method example

By Lokesh Gupta | Filed Under: Java String class

The Java String contains() searches a substring in the given string. It returns true if substring are found in this string otherwise returns false.

1. String contains() method

Use String.contains() to find is a substring is present in given string or not. Please remember that this method is case sensitive.

1.1. Method syntax

The method internally takes help of indexOf() method to check the index of substring. If substring is present then index will be always greater than '0'.

/**
* @param substring - substring to be searched in this string
* 
* @return - true if substring is found,
* 			false if substring is not found
*/
public boolean contains(CharSequence substring) {
    return indexOf(s.toString()) > -1;
}

1.2. ‘null’ is not valid method argument

String.contains() method does not accept 'null' argument. It will throw NullPointerException in case method argument is null.

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.contains(String.java:2120)
	at com.StringExample.main(StringExample.java:7)

2. String contains() example

Java program to find a substring is present in given string or not.

public class StringExample 
{
    public static void main(String[] args) 
    {
        System.out.println("Hello World".contains("Hello"));	//true
        
        System.out.println("Hello World".contains("World"));	//true

        System.out.println("Hello World".contains("WORLD"));	//false - case-sensitive
        
        System.out.println("Hello World".contains("Java"));		//false
    }
}

Program output.

true
true
false
false

In this example, we learned to check is substring is present in given string using Java string contains() method.

References:

A Guide to Java String
String Java Doc

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz