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:
Setting Up Swagger 2 with a Spring REST API
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Check for balanced parenthesis by using Stacks
Guide to ThreadLocalRandom in Java
Java Program to Implement Disjoint Set Data Structure
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Spring Boot - Introduction
A Guide to the Java ExecutorService
So sánh Array và ArrayList trong Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Serverless Functions with Spring Cloud Function
The Spring @Controller and @RestController Annotations
Spring 5 Functional Bean Registration
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Spring Cloud Connectors and Heroku
The Basics of Java Security
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Collections Interview Questions
Java Program to Implement Lloyd’s Algorithm
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Solve TSP Using Minimum Spanning Trees
Uploading MultipartFile with Spring RestTemplate
OAuth2.0 and Dynamic Client Registration
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Comparing Two HashMaps in Java
Java Program to Perform Searching Based on Locality of Reference
Hướng dẫn tạo và sử dụng ThreadPool trong Java
A Guide to JPA with Spring
Converting String to Stream of chars
Java Program to Solve any Linear Equations
Using Optional with Jackson
Java Program to Implement RoleList API