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:
Interface trong Java 8 – Default method và Static method
Java 8 Streams peek() API
Java Program to Implement Gale Shapley Algorithm
Base64 encoding và decoding trong Java 8
Checking for Empty or Blank Strings in Java
Registration with Spring Security – Password Encoding
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Spring Boot - Service Components
How to Add a Single Element to a Stream
Unsatisfied Dependency in Spring
Java Program to Find the Minimum value of Binary Search Tree
Add Multiple Items to an Java ArrayList
Spring Boot Application as a Service
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Check if it is a Sparse Matrix
Guide to the Volatile Keyword in Java
The Basics of Java Security
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Use rand and srand Functions
HttpClient Basic Authentication
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Zipping Collections in Java
How to Use if/else Logic in Java 8 Streams
How to Read HTTP Headers in Spring REST Controllers
Java Program to Perform Matrix Multiplication
HttpClient 4 – Follow Redirects for POST
Comparing Dates in Java
DistinctBy in the Java Stream API
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Introduction to the Java NIO Selector
Introduction to Spring Cloud Stream
Java Program to Find Path Between Two Nodes in a Graph