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:
Java Program to Implement Ternary Search Tree
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java – Reader to InputStream
Jackson vs Gson
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Perform Uniform Binary Search
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Hash Tables with Double Hashing
Feign – Tạo ứng dụng Java RESTful Client
Lớp HashMap trong Java
Java Program to Implement Tarjan Algorithm
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Implement Suffix Tree
The “final” Keyword in Java
Programmatic Transaction Management in Spring
ETags for REST with Spring
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Compute Cross Product of Two Vectors
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Tránh lỗi NullPointerException trong Java như thế nào?
SOAP Web service: Authentication trong JAX-WS
Concurrent Test Execution in Spring 5
Java Program to Implement TreeMap API
Convert char to String in Java
Java Program to Implement Fibonacci Heap
Lập trình đa luồng với CompletableFuture trong Java 8
A Guide to @RepeatedTest in Junit 5
An Intro to Spring Cloud Task