Java Program to Perform String Matching Using String Library

This is a java program to perform search using string library.

Here is the source code of the Java Program to Perform String Matching Using String Library. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

package com.maixuanviet.setandstring;
 
import java.util.Scanner;
 
public class StringSearchUsingStrLib
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the main string: ");
        String text = sc.nextLine();
        System.out.println("Enter the pattern string: ");
        String pattern = sc.nextLine();
        for (int i = 0; i <= (text.length() - pattern.length()); i++)
        {
            if (text.substring(i, (i + pattern.length())).equalsIgnoreCase(
                    pattern))
            {
                System.out.println(pattern
                        + " is substring of main string, match found at: "
                        + ++i);
            }
        }
        sc.close();
    }
}

Output:

$ javac StringSearchUsingStrLib.java
$ java StringSearchUsingStrLib
 
Enter the main string: 
Java Program to Perform String Matching Using String Library
Enter the pattern string: 
String
String is substring of main string, match found at: 25
String is substring of main string, match found at: 47

Related posts:

Checking for Empty or Blank Strings in Java
A Custom Data Binder in Spring MVC
Reversing a Linked List in Java
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java – Reader to Byte Array
LinkedList trong java
Database Migrations with Flyway
Spring Boot - Web Socket
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Spring Security – Reset Your Password
Check if a String is a Palindrome in Java
Validations for Enum Types
How To Serialize and Deserialize Enums with Jackson
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Spring Security Custom AuthenticationFailureHandler
Guide to Spring 5 WebFlux
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
REST Web service: Basic Authentication trong Jersey 2.x
Java Program to Find Number of Articulation points in a Graph
Spring WebClient Requests with Parameters
Error Handling for REST with Spring
Java equals() and hashCode() Contracts
“Stream has already been operated upon or closed” Exception in Java
Lập trình mạng với java
Spring REST with a Zuul Proxy
Java Program to Describe the Representation of Graph using Adjacency List
Creating Docker Images with Spring Boot
Java Program to implement Associate Array
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Spring Security Basic Authentication
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)