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 Quick Sort with Given Complexity Constraint
Java Program to Implement the Hill Cypher
Instance Profile Credentials using Spring Cloud
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Vector trong Java
Guide to Selenium with JUnit / TestNG
@Lookup Annotation in Spring
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program to Implement Self organizing List
Servlet 3 Async Support with Spring MVC and Spring Security
Get the workstation name or IP
Java Program to Perform Polygon Containment Test
Hướng dẫn Java Design Pattern – Visitor
Lớp Collectors trong Java 8
Default Password Encoder in Spring Security 5
Java Program to Implement Caesar Cypher
Java Program to Implement Floyd Cycle Algorithm
Java Convenience Factory Methods for Collections
Base64 encoding và decoding trong Java 8
Java Program to Find kth Largest Element in a Sequence
Show Hibernate/JPA SQL Statements from Spring Boot
Entity To DTO Conversion for a Spring REST API
String Operations with Java Streams
Jackson Ignore Properties on Marshalling
Send email with SMTPS (eg. Google GMail)
Java Program to Implement LinkedBlockingQueue API
A Guide to the ResourceBundle
Handling Errors in Spring WebFlux
Versioning a REST API
Java Program to Implement LinkedList API
Java Program to implement Bi Directional Map
Spring @Primary Annotation