Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equivalent of python string slicing

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
like image 370
Michael Swartz Avatar asked Sep 01 '15 09:09

Michael Swartz


People also ask

Does JavaScript have string slicing?

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.

What is slice command in JavaScript?

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.

What is slicing of string in python?

Slicing StringsSpecify the start index and the end index, separated by a colon, to return a part of the string.

Is slicing in python inclusive or exclusive?

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.


1 Answers

Simply use s2.slice(1) without the comma.

like image 104
Lauromine Avatar answered Oct 12 '22 13:10

Lauromine