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:
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
LinkedHashSet trong java
Performance Difference Between save() and saveAll() in Spring Data
Từ khóa this và super trong Java
Java Program to Implement Sieve Of Eratosthenes
A Guide to WatchService in Java NIO2
The Registration Process With Spring Security
Vector trong Java
Java NIO2 Path API
Converting String to Stream of chars
Java 8 Streams peek() API
Giới thiệu luồng vào ra (I/O) trong Java
Java – Reader to InputStream
Hướng dẫn Java Design Pattern – Intercepting Filter
Entity To DTO Conversion for a Spring REST API
Spring Boot Application as a Service
Using the Not Operator in If Conditions in Java
Java Streams vs Vavr Streams
Derived Query Methods in Spring Data JPA Repositories
Lớp LinkedHashMap trong Java
Các kiểu dữ liệu trong java
Removing all Nulls from a List in Java
Guide to the Java Queue Interface
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Implement Borwein Algorithm
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Perform Stooge Sort
Mệnh đề if-else trong java
Command-Line Arguments in Java
Java Program to Perform Finite State Automaton based Search
Introduction to Java Serialization