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:
A Guide to Java SynchronousQueue
Runnable vs. Callable in Java
Java 8 – Powerful Comparison with Lambdas
Extract network card address
The Spring @Controller and @RestController Annotations
Get the workstation name or IP
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Spring Boot - Web Socket
New Features in Java 13
New Features in Java 10
Chuyển đổi giữa các kiểu dữ liệu trong Java
Exception Handling in Java
Control the Session with Spring Security
Introduction to Spring Cloud OpenFeign
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to Implement Coppersmith Freivald’s Algorithm
More Jackson Annotations
Guide to Spring 5 WebFlux
Daemon Threads in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Annotation trong Java 8
Beans and Dependency Injection
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Guide to Mustache with Spring Boot
Java Program to Represent Graph Using Incidence List
Guide to Java 8’s Collectors
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement Skew Heap
Java 8 Collectors toMap
Spring Boot - Scheduling
How to Get a Name of a Method Being Executed?