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:
Abstract class và Interface trong Java
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Comparing Dates in Java
Disable DNS caching
Custom Error Pages with Spring MVC
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement Sorted Vector
Guide to the Java Queue Interface
Java Program to implement Sparse Vector
Java Program to Implement Repeated Squaring Algorithm
Spring Security Login Page with React
Introduction to Spring Cloud CLI
Java – Reader to InputStream
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Generate Date Between Given Range
Custom HTTP Header with the HttpClient
Comparing Long Values in Java
Introduction to Spring Data JPA
Creating a Custom Starter with Spring Boot
Java Program to Check whether Directed Graph is Connected using BFS
Function trong Java 8
Spring Boot - Tomcat Port Number
Sorting in Java
Java Program to Solve the 0-1 Knapsack Problem
Hướng dẫn Java Design Pattern – Builder
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Request a Delivery / Read Receipt in Javamail
More Jackson Annotations
Tính kế thừa (Inheritance) trong java
Java – String to Reader
Java Program to Check Cycle in a Graph using Graph traversal
REST Web service: Upload và Download file với Jersey 2.x