TypeScript | String substring() Method

The substring() is an inbuilt function in TypeScript which is used to return the subset of a String object.

 Syntax: 

string.substring(indexA, [indexB]) 

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

  • indexA : This parameter is the integer between 0 and one less than the length of the string.
  • indexB : This parameter is the integer between 0 and length of the string.

Return Value: This method returns the subset of a String object. 
Below example illustrate the String substring() method in TypeScript.

Example 1:  



JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Original strings
    var str = "Geeksforgeeks - Best Platform"
   
    // use of String substring() Method
    var newstr = str.substring(0,5) 
   
    console.log(newstr);
</script>

chevron_right


Output: 

Geeks

Example 2: 

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Original strings
    var str = "Geeksforgeeks - Best Platform"
   
    // use of String substring() Method
    var newstr = str.substring(0,5) 
    console.log(newstr);
   
    newstr = str.substring(-2,5) 
    console.log(newstr);
   
    newstr = str.substring(12,22) 
    console.log(newstr);
   
    newstr = str.substring(0,1) 
    console.log(newstr);
</script>

chevron_right


Output: 

Geeks
Geeks
s - Best P
G

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.