1const string = "0123456789";
2console.log(string.slice(0, 2)); // "01"
3console.log(string.slice(0, 8)); // "01234567"
4console.log(string.slice(3, 7)); // "3456"
1const str = 'substr';
2
3console.log(str.substr(1, 2)); // (1, 2): ub
4console.log(str.substr(1)); // (1): ubstr
5
6/* Percorrendo de trás para frente */
7console.log(str.substr(-3, 2)); // (-3, 2): st
8console.log(str.substr(-3)); // (-3): str
9
1let str = "abcdefghi"
2// index-> 012456789
3
4// str.slice(±start, ±end)
5console.log(str.slice(2, 5)) // cde
6console.log(str.slice(-5, -3)) // ef
7
8// str.subString(+start, +end) // can't take -ve index
9console.log(str.substring(2, 5)) // cde
10console.log(str.substring(5, 2)) // cde
11
12// str.substr(±start, length)
13console.log(str.substr(2, 5)) // cdefg
1//str.substr(start[, length])
2var str = 'abcdefghij';
3
4console.log('(1, 2): ' + str.substr(1, 2)); // '(1, 2): bc'
5console.log('(-3, 2): ' + str.substr(-3, 2)); // '(-3, 2): hi'
6console.log('(-3): ' + str.substr(-3)); // '(-3): hij'
7console.log('(1): ' + str.substr(1)); // '(1): bcdefghij'
8console.log('(-20, 2): ' + str.substr(-20, 2)); // '(-20, 2): ab'
9console.log('(20, 2): ' + str.substr(20, 2)); // '(20, 2): '
1//substr() is similar to slice(). The difference is that the second parameter specifies the length of the extracted part.
2let str = "Apple, Banana, Kiwi";
3let part = str.substr(7, 6);