How To Check If A String Contains A Particular Substring In Java

Chapter: String Handling Last Updated: 22-08-2023 17:19:49 UTC

Program:

            /* ............... START ............... */
                
// Using String.contains() Method:

String mainString = "Hello, world!";
String substring = "world";

boolean containsSubstring = mainString.contains(substring);

if (containsSubstring) {
    System.out.println("Substring found!");
} else {
    System.out.println("Substring not found.");
}


// Using String.indexOf() Method:

String mainString = "Hello, world!";
String substring = "world";

int index = mainString.indexOf(substring);

if (index != -1) {
    System.out.println("Substring found at index " + index);
} else {
    System.out.println("Substring not found.");
}


// Using Regular Expressions (Pattern and Matcher):

import java.util.regex.Pattern;
import java.util.regex.Matcher;

String mainString = "Hello, world!";
String regex = "world";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(mainString);

if (matcher.find()) {
    System.out.println("Substring found!");
} else {
    System.out.println("Substring not found.");
}


// Using Java 8+ Stream API:

String mainString = "Hello, world!";
String substring = "world";

boolean containsSubstring = mainString.chars()
        .mapToObj(c -> String.valueOf((char) c))
        .collect(Collectors.joining())
        .contains(substring);

if (containsSubstring) {
    System.out.println("Substring found!");
} else {
    System.out.println("Substring not found.");
}




                /* ............... END ............... */
        

Notes:

  • Using String.contains() Method: The String class in Java provides a method called contains() that can be used to check if a string contains a specified substring.
  • Using String.indexOf() Method: The indexOf() method returns the index of the first occurrence of a specified substring within the string. If the substring is not found, it returns -1.
  • Using Regular Expressions (Pattern and Matcher): If you need more complex pattern matching, you can use regular expressions with the Pattern and Matcher classes.
  • Using Java 8+ Stream API: If you are using Java 8 or later, you can use the Stream API to check if a string contains a substring.
  • Choose the method that best suits your needs based on the complexity of the substring search you're performing. The first two methods are simpler and sufficient for most cases, while the regular expression approach and Stream API approach offer more flexibility for advanced scenarios.

Tags

How to check if a string contains a particular substring in java #Java Program to Check if a string contains a substring

Similar Programs Chapter Last Updated
How To Split A String Into Substrings in Java String Handling 22-08-2023
How To Remove A Substring From A String Java String Handling 22-08-2023
Java Program To Remove Repeated Characters In A String String Handling 15-08-2023
Java Code To Check If String Contains Specific Characters String Handling 03-06-2023
How To Count The Number Of Cccurrences Of Each Character In A String In Java String Handling 03-06-2023
Java Program To Replace A Character In A String String Handling 03-06-2023
Java Program To Split A String String Handling 03-06-2023
String Length Implementation Java String Handling 02-12-2019

1