Javascript Program To Check If A String Is Substring Of Another Last Updated : 20 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: 5 Explanation: 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" Simple Approach: The idea is to 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 run another loop checking for sub-string from every index. For example, consider there to be a string of length N and a substring of length M. Then run a nested loop, where the outer loop runs from 0 to (N-M) and the inner loop from 0 to M. For very index check if the sub-string traversed by the inner loop is the given sub-string or not. JavaScript <script> // JavaScript program to check if a // string is substring of other. // Returns true if s1 is substring // of s2 function isSubstring(s1, s2) { var M = s1.length; var N = s2.length; /* A loop to slide pat[] one by one */ for (var i = 0; i <= N - M; i++) { var j; /* For current index i, check for pattern match */ for (j = 0; j < M; j++) if (s2[i + j] != s1[j]) break; if (j == M) return i; } return -1; } // Driver code var s1 = "for"; var s2 = "geeksforgeeks"; var res = isSubstring(s1, s2); if (res == -1) document.write("Not present"); else document.write( "Present at index " + res); </script> Output:Present 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. An efficient solution is to use a O(n) searching algorithm like KMP algorithm, Z algorithm, etc. Please refer complete article on Check if a string is substring of another for more details! Comment More infoAdvertise with us Next Article Javascript Program To Check If A String Is Substring Of Another kartik Follow Improve Article Tags : Strings Pattern Searching Web Technologies DSA Practice Tags : Pattern SearchingStrings Similar Reads Check if a string is substring of another Given two strings txt and pat, the task is to find if pat is a substring of txt. If yes, return the index of the first occurrence, else return -1.Examples : Input: txt = "geeksforgeeks", pat = "eks"Output: 2Explanation: String "eks" is present at index 2 and 9, so 2 is the smallest index.Input: txt 8 min read How to check a string is entirely made up of the same substring in JavaScript ? We are given a string and the task is to determine whether the string is made up of the same substrings or not. 1. Using Regular Expression with test() method Initialize a string to the variable.Use the test() method to test a pattern in a string.The test() method returns true if the match is found, 3 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read Check if the given string is shuffled substring of another string Given strings str1 and str2. The task is to find if str1 is a substring in the shuffled form of str2 or not. Print "YES" if str1 is a substring in shuffled form of str2 else print "NO". Example Input: str1 = "onetwofour", str2 = "hellofourtwooneworld" Output: YES Explanation: str1 is substring in sh 15 min read PHP to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo 2 min read Queries to check if string B exists as substring in string A Given two strings A, B and some queries consisting of an integer i, the task is to check whether the sub-string of A starting from index i and ending at index i + length(B) - 1 equals B or not. If equal then print Yes else print No. Note that i + length(B) will always be smaller than length(A). Exam 15+ min read How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri 1 min read Check if String formed by first and last X characters of a String is a Palindrome Given a string str and an integer X. The task is to find whether the first X characters of both string str and reversed string str are same or not. If it is equal then print true, otherwise print false. Examples: Input: str = abcdefba, X = 2Output: trueExplanation: First 2 characters of both string 5 min read Check if a string can be converted to another given string by removal of a substring Given two strings S and T of length N and M respectively, the task is to check if the string S can be converted to the string T by removing at most one substring of the string S. If found to be true, then print âYESâ. Otherwise, print âNOâ. Example: Input: S = âabcdefâ, T = âabcâ Output: YES Explana 7 min read Searching For Characters and Substring in a String in Java Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java.Searching for a Character in a 5 min read Like