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 Compute the Volume of a Tetrahedron Using Determinants
How to Get All Spring-Managed Beans?
Working With Maps Using Streams
Java Program to Implement PriorityQueue API
Introduction to Java Serialization
Object cloning trong java
Java Program to Implement Coppersmith Freivald’s Algorithm
Logging a Reactive Sequence
Spring Boot - Actuator
Converting String to Stream of chars
A Guide to HashSet in Java
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Copy Constructor
Java Program to Implement ArrayList API
Quick Guide to Spring MVC with Velocity
Java – Rename or Move a File
Java Program to Implement ScapeGoat Tree
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Implement the One Time Pad Algorithm
Java Program to Implement Graph Structured Stack
Quick Guide to @RestClientTest in Spring Boot
Join and Split Arrays and Collections in Java
Introduction to Spring Data REST
Java Program to Create a Balanced Binary Tree of the Incoming Data
Hướng dẫn Java Design Pattern – Intercepting Filter
Copy a List to Another List in Java
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Optimize Wire Length in Electrical Circuit
A Guide to Java SynchronousQueue
Phân biệt JVM, JRE, JDK
A Custom Media Type for a Spring REST API
Hướng dẫn Java Design Pattern – Adapter