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 the Hungarian Algorithm for Bipartite Matching
Custom Cascading in Spring Data MongoDB
HttpClient with SSL
Spring Boot - Build Systems
How to Break from Java Stream forEach
Removing Elements from Java Collections
Java Program to Implement LinkedBlockingDeque API
Java equals() and hashCode() Contracts
Java Program to Check the Connectivity of Graph Using BFS
Java Program to Implement K Way Merge Algorithm
Introduction to Spring Boot CLI
Spring Cloud AWS – EC2
Java Program to Represent Graph Using 2D Arrays
Constructor Dependency Injection in Spring
Supplier trong Java 8
Java Program to Perform Arithmetic Operations on Numbers of Size
Guide to Guava Table
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Implement Johnson’s Algorithm
Java Program to Describe the Representation of Graph using Incidence List
Notify User of Login From New Device or Location
A Guide to Spring Cloud Netflix – Hystrix
Spring Cloud – Adding Angular
Java Program to Implement Circular Singly Linked List
Lập trình mạng với java
Java Program to Implement Queue using Linked List
Spring Boot - Bootstrapping
Java Program to Implement Hash Tree
Java Program to Implement Gauss Seidel Method
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Spring Boot Integration Testing with Embedded MongoDB