commit
c06f0dd625
1 changed files with 4 additions and 4 deletions
|
@ -394,7 +394,7 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
|
|||
|
||||
```js run
|
||||
let str = "st*!*ringify*/!*";
|
||||
alert( str.slice(2) ); // ringify, from the 2nd position till the end
|
||||
alert( str.slice(2) ); // 'ringify', from the 2nd position till the end
|
||||
```
|
||||
|
||||
Negative values for `start/end` are also possible. They mean the position is counted from the string end:
|
||||
|
@ -403,7 +403,7 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
|
|||
let str = "strin*!*gif*/!*y";
|
||||
|
||||
// start at the 4th position from the right, end at the 1st from the right
|
||||
alert( str.slice(-4, -1) ); // gif
|
||||
alert( str.slice(-4, -1) ); // 'gif'
|
||||
```
|
||||
|
||||
`str.substring(start [, end])`
|
||||
|
@ -435,14 +435,14 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
|
|||
|
||||
```js run
|
||||
let str = "st*!*ring*/!*ify";
|
||||
alert( str.substr(2, 4) ); // ring, from the 2nd position get 4 characters
|
||||
alert( str.substr(2, 4) ); // 'ring', from the 2nd position get 4 characters
|
||||
```
|
||||
|
||||
The first argument may be negative, to count from the end:
|
||||
|
||||
```js run
|
||||
let str = "strin*!*gi*/!*fy";
|
||||
alert( str.substr(-4, 2) ); // gi, from the 4th position get 2 characters
|
||||
alert( str.substr(-4, 2) ); // 'gi', from the 4th position get 2 characters
|
||||
```
|
||||
|
||||
Let's recap these methods to avoid any confusion:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue