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 Stack API
Intersection of Two Lists in Java
Collection trong java
Jackson – Marshall String to JsonNode
Fixing 401s with CORS Preflights and Spring Security
Java Program to Implement Trie
Java Program to Implement the Monoalphabetic Cypher
Introduction to Spring Method Security
Quick Guide to Spring Controllers
Join and Split Arrays and Collections in Java
Anonymous Classes in Java
Spring Boot - Cloud Configuration Client
Java Program to Implement Depth-limited Search
Convert Time to Milliseconds in Java
Quick Guide to @RestClientTest in Spring Boot
Spring WebClient Filters
Function trong Java 8
Jackson – Bidirectional Relationships
Arrays.asList vs new ArrayList(Arrays.asList())
Guide to Apache Commons CircularFifoQueue
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Service Registration with Eureka
Java Program to Compute the Area of a Triangle Using Determinants
Lập trình mạng với java
Check If a String Is Numeric in Java
Java Program to Implement VList
Java Program to implement Associate Array
Tìm hiểu về Web Service
Java Program to Implement SynchronosQueue API
Introduction to the Java ArrayDeque
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Implement Dijkstra’s Algorithm using Set