• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Check if a String Contains a Substring in Java

July 23, 2023 //  by Talha Malik

While dealing with data/record handling in Java, there can arise a scenario that requires the developer to omit the duplication of values. For instance, omitting the repeated substrings in the strings or checking for a target value i.e., substring from a record of strings. In such situations, analyzing the substrings in a string is assistive in sorting out data handling issues effectively.

This guide will state the approaches to check if a string comprises a substring in Java.

How to Check/Verify if a String Contains a Substring in Java?

To check for a substring in a string, consider the below-stated approaches:

  • “contains()” Method.
  • “indexOf()” Method.

Approach 1: Checking if a String Contains a Substring in Java Using the “contains()” Method

The “contains()” method verifies if a string contains a sequence of characters. It gives “true” if the characters are contained and false otherwise. This method checks for a target substring in the string and returns the corresponding Boolean outcome.

Syntax

public boolean contains(char)

In this syntax, “char” represents the characters that need to be searched.

Now, proceed to the below-given code lines:

public class Substring {
public static void main(String[] args) {
  String stringValue = "This is Linuxhint";
  String sub1 = "Linuxhint";
  boolean out = stringValue.contains(sub1);
  if(out) {
  System.out.println("The substring (" +sub1 + ") is contained in the string!");
}
  else {
  System.out.println("The substring (" +sub1 + ") is not contained in the string!");
}
}}

In this code:

  • Initialize the string comprising the stated substrings.
  • In the next step, define another string (a substring in the formerly defined string).
  • Now, apply the “contains()” method to check if the latter string is contained as a substring in the former string.
  • Lastly, apply the “if” statement that invokes the “if” condition if the substring is contained in the string.
  • Otherwise, the “else” statement comes into effect.

Output

This output implies that the defined substring is contained in the string.

“else” Condition

Below is the demonstration of the “else” condition (substring not contained in the string):

Approach 2: Checking if a String Contains a Substring in Java Using the “indexOf()” Method

The “indexOf()” method gives the index of the first occurrence of a specified character(s) or a substring in a string. This method can be implemented to apply the discussed check based on the index of a substring in the initialized string.

Syntax 

indexOf(char, ind)

In the given syntax:

  • “char” points to the index of the character or string to locate.
  • “ind” is the index form where to start the search.

Note: This method gives the index of the string character’s first occurrence, or “-1” if not found.

Now, move on to the below-provided demonstration:

public class Substring {
public static void main(String[] args) {
  String stringValue = "This is Linuxhint";
  String sub1 = "Linuxhint";
  Integer out = stringValue.indexOf(sub1);
  if(out == -1) {
  System.out.println("The substring (" +sub1 + ") is not contained in the string!");
}
  else {
  System.out.println("The substring (" +sub1 + ") is contained in the string!");
}
}}

In this block of code:

  • Likewise, initialize a string and a substring, respectively.
  • Now, associate the “indexOf()” method with the former defined string having the latter string as its argument to check for the index of the substring in the string.
  • After that, utilize the “if/else” statement such that if the index of the substring is not found i.e., -1 in the string, the “if” statement is invoked.
  • Otherwise, the “else” statement executes.

Output

Here, it can be visualized that the string comprises the substring.

“else” Condition

Following is the demonstration of the invoked “else” condition when the string does not comprise the defined substring:

Conclusion

To check for a substring in a string in Java, apply the “contains()” method or the “indexOf()” method. These approaches evaluate the corresponding outcome based on the returned Boolean values or the method’s return type, respectively.

Category: Java

Previous Post: « Enhanced for Loop in Java
Next Post: How to Check if a String Contains Only Digits in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact