I always found the Python methodology of selecting characters from strings (or
items from arrays) to be the most natural I've found among a number of language
solutions. Was considering whether adding the syntax would be beneficial for
JavaScript. For those who do not know, the syntax is as so:
```js
const str = 'example'
const arr = ['e', 'x', 'a', 'm', 'p', 'l', 'e']
// Single elements are selected as in JS:
str[1] // 'x'
arr[2] // 'a'
// Multiple elements are selected by providing two numbers, delimited by a colon
// The first number is inclusive, the last number is exclusive
str[1:3] // 'xa'
arr[3:6] // ['m', 'p', 'l']
// These numbers can also be negative, resulting in indexing from the end of
the string/array
str[-1] // 'e'
str[-3:-1] // 'pl'
arr[-5:-3] // ['a', 'm']
arr[0:-5] // ['e', 'x']
// There is also a third number, which steps through the selection chosen by
the first two
str[0:5:2] // 'eap'
// This number can be negative, which results in stepping backwards through the
string/array
str[::-1] // 'elpmaxe'
// An empty item represents one of the following based upon it's position:
[0:length:1]
```
The most official documentation I could find on this topic is from the Python
tutorial [https://docs.python.org/3.6/tutorial/introduction.html#strings], a
more useful reference however might be the Stack Overflow post on the topic
[https://stackoverflow.com/questions/509211/explain-slice-notation].
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss