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 Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Construct an Expression Tree for an Prefix Expression
Spring Boot - Bootstrapping
Java Program to Check whether Directed Graph is Connected using DFS
Spring Boot - Scheduling
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Jackson – Marshall String to JsonNode
So sánh ArrayList và LinkedList trong Java
Java Program to Create a Random Graph Using Random Edge Generation
Object cloning trong java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Receive email using IMAP
Java Program to Implement Selection Sort
Lập trình đa luồng với Callable và Future trong Java
Ép kiểu trong Java (Type casting)
A Guide to System.exit()
How to Round a Number to N Decimal Places in Java
New Stream Collectors in Java 9
Java Program to Implement IdentityHashMap API
Java Program for Douglas-Peucker Algorithm Implementation
JWT – Token-based Authentication trong Jersey 2.x
Multipart Upload with HttpClient 4
Java Program to Convert a Decimal Number to Binary Number using Stacks
Explain about URL and HTTPS protocol
Java Program to Check if a Matrix is Invertible
Spring MVC Custom Validation
ETags for REST with Spring
A Guide to the ViewResolver in Spring MVC
Mapping a Dynamic JSON Object with Jackson
Hướng dẫn Java Design Pattern – Chain of Responsibility
Giới thiệu Java 8
Java Program to find the peak element of an array using Binary Search approach