Is there a JavaScript equivalent to this Python method of string slicing?
>>> 'stackoverflow'[1:]
'tackoverflow'
I have tried:
// this crashes
console.log("stackoverflow".slice(1,));
// output doesn't print the last letter 'w'
console.log("stackoverflow".slice(1, -1));
// tackoverflo
The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array. The original array will not be modified.
Slicing StringsSpecify the start index and the end index, separated by a colon, to return a part of the string.
Python is a zero-indexed language (things start counting from zero), and is also left inclusive, right exclusive you are when specifying a range of values.
Simply use s2.slice(1)
without the comma.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With