Java Program To Check If A String Is Substring Of Another Last Updated : 13 May, 2024 Comments Improve Suggest changes Like Article Like Report Write a java program for a given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks"Output: -1.Explanation: There is no occurrence of "practice" in"geeksforgeeks" Naive Approach: Run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop running another loop checking for sub-strings starting from every index. Follow the steps below to implement the idea: Run a for loop with counter i from 0 to N – M.Run a for loop with counter j from 0 to M-1.Compare jth character of S1 with (i+j)th character of S2.If the loop terminates after matching all the characters, then return i, i.e. substring S1 is found starting from ith character of S2Return -1 as no substring is found.Below is the Implementation of the above idea. Java // Java program to check if a string is // substring of other. class GFG { // Returns true if s1 is substring of s2 static int isSubstring(String s1, String s2) { int M = s1.length(); int N = s2.length(); /* A loop to slide pat[] one by one */ for (int i = 0; i <= N - M; i++) { int j; /* For current index i, check for pattern match */ for (j = 0; j < M; j++) if (s2.charAt(i + j) != s1.charAt(j)) break; if (j == M) return i; } return -1; } /* Driver code */ public static void main(String args[]) { String s1 = "for"; String s2 = "geeksforgeeks"; int res = isSubstring(s1, s2); if (res == -1) System.out.println("Not present"); else System.out.println("Present at index " + res); } } // This code is contributed by JaideepPyne. OutputPresent at index 5 Complexity Analysis: Time complexity: O(m * n) where m and n are lengths of s1 and s2 respectively. A nested loop is used the outer loop runs from 0 to N-M and inner loop from 0 to M so the complexity is O(m*n).Space Complexity: O(1). As no extra space is required.Java Program To Check If A String Is Substring Of Another using Java Library: The indexOf method in Java is used for finding the starting index of a substring in a given string. Below is the Implementation of above approach. Java // Java program to implement the approach class GFG { // function to get the index of s2 in s1 static int isSubstring(String s1, String s2) { // using indexOf method to check if s1 is // a substring of s2 return s2.indexOf(s1); } public static void main(String[] args) { String s1 = "for"; String s2 = "geeksforgeeks"; // Function Call int res = isSubstring(s1, s2); if (res == -1) System.out.println("Not present"); else System.out.println("Present at index " + res); } } // this code is contributed by phasing17 OutputPresent at index 5 Time Complexity: O(N) , where N is the length of the longer string s2 Auxiliary space: O(1) Please refer complete article on Check if a string is substring of another for more details! Comment More infoAdvertise with us Next Article Java Program To Check If A String Is Substring Of Another kartik Follow Improve Article Tags : Strings Pattern Searching Java Programs DSA Practice Tags : Pattern SearchingStrings Similar Reads How to Check if One String is a Rotation of Another in Java? In Java, to check if one string is a rotation of another, we can do string concatenation and substring matching. A rotated string is created by moving some characters from the beginning of the string to the end while maintaining the character order.Program to Check if One String is a Rotation of Ano 2 min read Java Program to Check if a string can be obtained by rotating another string 2 places Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Inter 2 min read Java Program to Check Whether a String is a Palindrome A string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look 8 min read Java Program to Find All Palindromic Sub-Strings of a String Given a string, the task is to count all palindrome substring in a given string. Input : aba Output : 4 Explanation : All palindrome substring are : "aba" , "a" , "b", "a" Input : TENET Output : 7 Explanation : All palindrome sub-string are : "T" , "E" , "N", "E", "T" , "ENE" , "TENET" Approach: Tak 2 min read Java Program to Check Whether the String Consists of Special Characters In Java, special characters refer to symbols other than letters and digits, such as @, #, !, etc. To check whether the String consists of special characters, there are multiple ways, including using the Character class, regular expressions, or simple string checks.Example:In this example, we will us 4 min read Extract a Substring Between Two Characters in a String in Java In Java, the extraction of the substring between the two characters in a string can be implemented with the substring method, and string is a pre-defined method in the class of the String. A String is a pre-defined class of the java.lang package. substring(startIndex, endIndex): This is a pre-define 2 min read Java Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read Java Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (lon 2 min read How to Split a String into Equal Length Substrings in Java? In Java, splitting a string into smaller substrings of equal length is useful for processing large strings in manageable pieces. We can do this with the substring method of the loop. This method extracts a substring of the specified length from the input string and stores it in a list.Example:In the 3 min read Split a String into a Number of Substrings in Java Given a String, the task it to split the String into a number of substrings. A String in java can be of 0 or more characters. Examples : (a) "" is a String in java with 0 character (b) "d" is a String in java with 1 character (c) "This is a sentence." is a string with 19 characters. Substring: A Str 5 min read Like