TypeScript | String slice() Method with example

The slice() is an inbuilt function in TypeScript which is used to extracts a section of a string and returns a new string. 
Syntax: 

string.slice( beginslice [, endSlice] )

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

  • beginSlice – This parameter is the zero-based index at which to begin extraction.
  • endSlice – This parameter is the zero-based index at which to end extraction.

Return Value: This method returns the index of the regular expression inside the string. Otherwise, it returns -1. 

Below example illustrate the  String slice() method in TypeScriptJS:

Example 1: 



JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

Geeksforgeeks

Example 2: 

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

Best Platform

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.