|
| 1 | + /// STRING METHODS /// |
| 2 | + // CHARAT() METHOD // |
| 3 | +// const str = 'Code Explained'; |
| 4 | +// console.log(str.charAt(0)); |
| 5 | + |
| 6 | +// // INDEXoF() AND LASTiNDEXoF() METHODS // |
| 7 | +// const s = 'The code undefined code code!'; |
| 8 | +// // string.indexOf(searchString,Position) |
| 9 | +// // string.lastIndexOf(searchString,Position); |
| 10 | +// console.log(s.indexOf('code')); |
| 11 | +// console.log(s.lastIndexOf('code')); |
| 12 | +// console.log(s.indexOf('code',5));//i am searching with position "5" |
| 13 | +// console.log(s.lastIndexOf('code',24));//i am searching with position "24" |
| 14 | +// console.log(s.indexOf(''));//with empty string it searches empty places from right to left |
| 15 | +// console.log(s.lastIndexOf(''));//with empty string it searches empty places from left to right |
| 16 | +// console.log(s.indexOf());//it searches "undefined" from right to left |
| 17 | +// console.log(s.lastIndexOf());////it searches "undefined" from left to right |
| 18 | + |
| 19 | + |
| 20 | + // SLICE() AND SUBSTRING() METHODS // |
| 21 | + |
| 22 | +// const s = 'The morning is upon us'; |
| 23 | + // sytnaxes |
| 24 | +// //string.slice(indexStart); |
| 25 | +// //string.substring(indexStart); |
| 26 | +// //string.slice(indexStart,indexEnd); |
| 27 | +// //string.substring(indexStart,indexEnd); |
| 28 | + |
| 29 | +// console.log(s.slice(12));//get indexes from position "12" till end |
| 30 | +// console.log(s.substring(12));//get indexes from position "12" till end |
| 31 | +// console.log(s.slice(-11));//same result with positive get from "-11" till "-1" |
| 32 | +// console.log(s.substring(-11));//not same when we have negative number "substring" change indexStart to zero and get from position "0" till end. |
| 33 | +// console.log(s.slice(13,16));// it gets index from "13" position but not include "16"; |
| 34 | +// console.log(s.substring(13,16));//it gets index from "13" position but not include "16"; |
| 35 | +// console.log(s.slice(16,13));//it return empty string |
| 36 | +// console.log(s.substring(16,13));//if "indexstart" is bigger than "endIndex" then they swapped |
| 37 | +// console.log(s.slice(-8,-4));//also we can work negative numbers |
| 38 | +// console.log(s.substring(-8,-4));//both of numbers change to "0" it returns empty string |
| 39 | +// console.log(s.slice(-8,4));//it returns empty string because indexstart postion is on the first than endindex position |
| 40 | +// console.log(s.substring(-8,4));//first negative number change to zero |
| 41 | +// console.log(s.slice(8,-4)); |
| 42 | +// console.log(s.substring(8,-4));//first negative number change to zero than compare numbers if endIndex is less than indexStart than swapped their place |
0 commit comments