TypeScript | String indexOf() Method

The indexOf() is an inbuilt function in TypeScript which is used to get the index within the calling String object of the first occurrence of the specified value. 
Syntax:

string.indexOf(searchValue[, fromIndex]) 

Parameter: This method accepts two parameter as mentioned above and described below . 

  • searchValue: This parameter is a string representing the value to search for.
  • fromIndex: This parameter is a location within the calling string to start the search from.

Return Value: This method returns the index of the found occurrence, otherwise -1 if not found.

Below example illustrate the  String indexOf() method in TypeScriptJS:
Example 1: 

JavaScript



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Original strings
    var str1 = new String('TypeScript | \
String indexOf() Method with example'); 
  
    // use of String indexOf() Method
    console.log(str1.indexOf("String"));
</script>

chevron_right


Output: 

13

Example 2: 

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Original strings
    var str1 = new String('TypeScript | \
    String indexOf() Method with example'); 
  
    // use of String indexOf() Method
    console.log(str1.indexOf("Geeks"));
</script>

chevron_right


Output: 

-1

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.