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:
Working with Tree Model Nodes in Jackson
Hướng dẫn Java Design Pattern – Command
Spring Boot With H2 Database
Introduction to Java 8 Streams
Removing all duplicates from a List in Java
Spring MVC Tutorial
Java Program to Implement Find all Cross Edges in a Graph
Spring Data – CrudRepository save() Method
Java String Conversions
Logging in Spring Boot
Intersection of Two Lists in Java
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Immutable Objects in Java
Java Program to Implement Gaussian Elimination Algorithm
Lập trình mạng với java
Getting Started with Custom Deserialization in Jackson
Spring Boot - Internationalization
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Describe the Representation of Graph using Incidence Matrix
Extra Login Fields with Spring Security
A Guide To UDP In Java
Basic Authentication with the RestTemplate
The HttpMediaTypeNotAcceptableException in Spring MVC
Spring Boot - Admin Server
Java Program to Implement Word Wrap Problem
Overview of Spring Boot Dev Tools
Java Program to add two large numbers using Linked List
Java Program to Find Transitive Closure of a Graph
@Order in Spring
Java equals() and hashCode() Contracts
Recommended Package Structure of a Spring Boot Project
Spring Security 5 – OAuth2 Login