Write a java program to find a substring in string

Problem Statement

Given three strings say Searchstring, Str1, and Str2 as input, write a Java program to find out if Str2 comes after Str1 in the Searchstring.

The 3 arguments correspond to SearchString, Str1, and Str2. The function returns 1 if Str2 appears after Str1 in the Searchtring. Else it returns 2.

Input and Output Format

  • Input consists of 3 strings.
  • The first input corresponds to the SearchString.
  • The second input corresponds to Str1.The third input corresponds to Str2.
  • The output consists of a string that is either “yes” or “no”

Sample Input 1:

geniousRajKumarDev

Raj

Dev

Sample Output 1:

yes

Sample Input 2:

geniousRajKumarDev

Dev

Raj
Sample Output 2:

No

Find a substring in the string in Java

Following are the steps to find a substring. We will be using the following two methods:

  1. contains(): It checks if the string contains another substring. It returns a boolean value.
  2. indexOf(): It checks if the string contains a particular character or not. If the string doesn’t contain a particular character then it will return -1.

  • Input three strings from the user and pass them to getvalues() method.
  • Inside the method, first checks if the first string contains the second string and third-string using contains() method.
  • Next, if it is true then check if the second string comes after the third then print “YES” otherwise print “NO”.
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    String s2 = sc.nextLine();
    String s3 = sc.nextLine();

    getvalues(s1, s2, s3);
  }

  public static void getvalues(String s1, String s2, String s3) {
    if (s1.contains(s2) && s1.contains(s3)) {
      if (s1.indexOf(s2) < s1.indexOf(s3)) {
        System.out.println("YES");
      } else
        System.out.println("No");
    }
  }
}

Output

Java Program to find SubString without using Library Function

Input and Output Format

  • Input consists of 2 strings. Input consists of string s1 and the string which we want to check is string s2.
  • The output consists of a boolean value.

Sample Input 1:

codebun

deb

Sample Output 1:

true

Sample Input 2:

codebun

abc

Sample Output 2:

false

Following are the steps to find substring without using library function:

  • Input two strings from the user and pass them to getvalues() method.
  • Inside the method, declare and initialize a variable current to 0.
  • Next, iterate over the string s1 and check if the character at ith position is equal to the character at the current position of string s2.
  • If it is true then increment the value of the current position and check if the current value is equal to the length of the string and hence return true else return false.
package com.demo;

import java.util.Scanner;

public class TestJava2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();
        System.out.println(getvalues(s1,s2));
    }

    public static boolean getvalues(String s1, String s2) {
        int current = 0;
        for(int i=0;i<s1.length();i++) {
            if(s1.charAt(i) == s2.charAt(current)) {
                current++;
                if(current == s2.length()) {
                    return true;
                }
            }else {
                current = 0;
            }
        }
        return false;
    }
}

Output

Thus, in this way, we learn how to find a substring in a string in Java with or without using the library function.