Which method returns the index within calling String object of first occurrence of specified value ? Last Updated : 09 Jan, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will know how to return the index of the first occurrence of the specified value(can be a string or character) in the string, also will understand their implementation through the examples. JavaScript indexOf() Method: The indexOf() method is a built-in & case-sensitive method that returns the index of the first occurrence of the specified value in the calling string object. It will return -1 if no such value is found. The main usage of this method is to find the existence of the particular string or character in the string because the method returns -1 if the provided search value is not present. This method can also be used to calculate the frequency of specific search value in the string. Syntax: indexOf(searchValue, index)Example 1: This example illustrates the search value that can be a string or any character. JavaScript let myString = "Hello, GeeksforGeeks Learner!"; console.log("The index of 'Hello' in string is: ", myString.indexOf("Hello")); console.log("The index of first occurrence of 'G' is: ", myString.indexOf('G')); Output: The index of 'Hello' in string is: 0 The index of first occurrence of 'G' is: 7Explanation: In the first line, we have declared a string with initialization, and later we are calling the indexOf() method with this string as a calling object by providing the "Hello" as an argument. Now, this method will return the first occurrence of "Hello" in the string which is 0. After then, we have searched for the character 'G' in the string. Here, this method will return 7 because that is the first occurrence. Example 2: This example describes finding the first occurrence of the argument character or string wrt to the specific position. HTML let myString = "Hi Javascript Developer"; console.log("The index of first occurrence 'i' from 3rd index is: ", myString.indexOf("i", 3)); console.log("The index of first occurrence 'Hi' from the 7th index is: ", myString.indexOf('Hi', 7)); Output: The index of first occurrence 'i' from 3rd index is: 10 The index of first occurrence 'Hi' from the 7th index is: -1Explanation: Here, we are calling the indexOf() method by providing the start index of searching explicitly, In the first one, we are calling the method by providing 'i' and 3. The 'i' will be found on the 10th index if the starting search index is 3. Similarly in the second one, we have provided the 'Hi' for searching from the 7th index but there is no occurrence of "Hi" after the 7th index hence -1 will be returned. Comment More infoAdvertise with us Next Article Which method returns the index within calling String object of first occurrence of specified value ? mrtwinklesharma Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads Which built-in method returns the characters in a string beginning at the specified location ? In this article, we will know how to return the required sequence of characters in a string, beginning from a specified position, in Javascript. There are 2 built-in methods to accomplish this task: Using the substr() MethodUsing the slice() MethodWe will explore these methods & will understand 3 min read How to Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1 4 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read JavaScript - How to Get the First Three Characters of a String? Here are the various methods to get the first three characters of a string in JavcaScript1. Using String.slice() MethodThe slice() method is one of the most commonly used and versatile methods to extract a part of a string. It allows you to specify the start and end positions for slicing the string. 3 min read How to Find the Index of an Element that Contains the Given Substring in Array ? To find the index of an element within an array that contains a specific substring in JavaScript, there are multiple strategies available. The methods offer flexibility in handling various scenarios, allowing you to efficiently locate the index of the desired substring within the array. Table of Con 2 min read TypeScript | String lastIndexOf() Method The lastIndexOf() is an inbuilt function in TypeScript which is used to get the index within the calling String object of the last occurrence of the specified value. Syntax: string.lastIndexOf(searchValue[, fromIndex]) Parameter: This method accepts two parameter as mentioned above and described bel 1 min read JavaScript - How to Check if a String "StartsWith" Another String in JavaScript? Here are the various methods to check if a string starts with another string in JavaScript1. Using String.startsWith() MethodThe most widely used method is the startsWith() method, built into JavaScript. This method is simple to use, highly readable, and performs the task efficiently.JavaScriptlet s 3 min read strings.Index() Function in Golang With Examples strings.Index() Function in Golang is used to get the first instance of a specified substring. If the substring is not found, then this method will return -1. Syntax: func Index(str, sbstr string) int Here, str is the original string and sbstr is a string whose we want to find index value. Example 1 2 min read strings.LastIndex() Function in Golang With Examples strings.LastIndex() function in the Golang returns the starting index of the occurrence of the last instance of a substring in a given string. If the substring is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the 2 min read C# - Different Ways to Find All Substrings in a String Given a string as an input we need to find all the substrings present in the given string. Example: Input: geeks Output: g e e k s ge ee ek ks gee eek eks geek eeks geeks Input: ab Output: a b abMethod 1: Using Substring() method We can find all the substrings from the given string using the Substri 3 min read Like