TypeScript String slice()
The slice() is an inbuilt function used to extract a section of a string and return a new string.
Syntax:
string.slice( beginslice [, endSlice] )
Parameters:
This method accepts two parameters 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.
Example 1:
// Original strings
let str = "Geeksforgeeks - Best Platform";
// use of String slice() Method
let newstr = str.slice(0,14);
console.log(newstr);
Output:
Geeksforgeeks
Example 2:
// Original strings
let str = "Geeksforgeeks - Best Platform";
// use of String slice() Method
let newstr = str.slice(16);
console.log(newstr);
Output:
Best Platform