TypeScript | String slice() Method with example
Last Updated :
18 Jun, 2020
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
<script>
var str = "Geeksforgeeks - Best Platform" ;
var newstr = str.slice(0,14);
console.log(newstr);
</script>
|
Output:
Geeksforgeeks
Example 2:
JavaScript
<script>
var str = "Geeksforgeeks - Best Platform" ;
var newstr = str.slice(16);
console.log(newstr);
</script>
|
Output:
Best Platform
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...