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 – Combine Multiple Collections
Receive email using IMAP
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Introduction to Project Reactor Bus
Java Program to Implement Find all Back Edges in a Graph
Java Program to Perform Partition of an Integer in All Possible Ways
Giới thiệu JDBC Connection Pool
Java Program to Optimize Wire Length in Electrical Circuit
Introduction to Spring Cloud Stream
Java Program to Implement Borwein Algorithm
Java Program to Implement Network Flow Problem
The Order of Tests in JUnit
HttpAsyncClient Tutorial
Create a Custom Auto-Configuration with Spring Boot
A Guide to Concurrent Queues in Java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement vector
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Introduction to Spliterator in Java
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Implement CopyOnWriteArrayList API
Java Program to Implement Strassen Algorithm
Creating a Generic Array in Java
Từ khóa this và super trong Java
Removing all Nulls from a List in Java
Java Program to implement Sparse Vector
REST Web service: Upload và Download file với Jersey 2.x
Guide to the Java ArrayList
Java Program to Implement Control Table
Spring Boot - Exception Handling
New Features in Java 15
Guide to @JsonFormat in Jackson