Strings Notebook
Strings
In JS index starts from zero but length starts from one. But same index when negatively rotated starts from one. Too much confusion traps.
IN [171]
let text = "RAHUL";
let length = text.length;
console.log(length) IN [172]
console.log(" >Good way: \n at(0) = "+text.at(0)+ "\n at(-1) = "+text.at(-1) +
"\n\n >Bad unpredictable way: \n [0] = "+text[0]+ " \n [-1] = "+text[-1])Sub string extract methods
- slice(startIndex, endIndex) // negative index is reversed array
- substring(startIndex, endIndex) // negative index converts to zero
- substr(startIndex, length) // negative index is reversed array
IN [173]
console.log(
" text = "+text+ " \n length: "+text.length+
"\n"+
"\n slice()"+
"\n slice(0, 2) = "+text.slice(0, 2)+" # this second endIndex is making it endIndex-1"+
"\n slice(0, 3) = "+text.slice(0, 3)+
"\n slice(1, 3) = "+text.slice(1, 3)+
"\n slice(2) = "+text.slice(2)+
"\n slice(-2) = "+text.slice(-2)+
"\n at(-5) = "+text.at(-5)+
"\n at(-1) = "+text.at(-1)+
"\n slice(-5, -1) = "+text.slice(-5, -1)+" # negative index is reversed array"+
"\n"+
"\n substring()"+
"\n substring(0, 2) = "+text.substring(0, 2)+
"\n substring(-1, 2) = "+text.substring(-1, 2)+" # negative index converts to zero"+
"\n substr(0, 5) = "+text.substr(1, 50)+
"\n substr(-5, 2) = "+text.substr(-5, 2)+" # negative index is reversed array"
) IN [174]
let text1 = " Hello World! ";
console.log(
" trim() = "+text1.trim()+
"\n trimStart() = "+text1.trimStart()+
"\n trimEnd() = "+text1.trimEnd()
) IN [175]
let numb = 5;
console.log(" numb.toString() = " +numb.toString()+
"\n String(numb) = "+String(numb)+
"\n type = "+typeof String(numb)
) IN [201]
// replace()
console.log(
" text = "+text+
"\n replace('H', 'J') = "+text.replace('H', "J")+
"\n replace('h', 'J') = "+text.replace('h', "J")+' # case sensitive'+
"\n replace(/h/i, 'J') = "+text.replace(/h/i, "J")+" # case insensitive"+
"\n replace(/H/g, 'J') = "+text.replace(/H/g, "J")+" regex"+
"\n replace(/h/gi, 'J') = "+text.replace(/h/gi, "J")+" regex case insensitive"
) IN [200]
// replaceAll()
let text2 = "Hello, World!";
console.log(
" text = "+text2+
"\n replaceAll('o', 'J') = "+text2.replaceAll('o', "J")+
"\n replaceAll(/o/g, 'J') = "+text2.replaceAll(/o/g, "J")+" regex"+
"\n replaceAll(/o/gi, 'J') = "+text2.replaceAll(/o/gi, "J")+" regex case insensitive"
) IN [212]
// split()
console.log(
"\n 'RAHUL'.split('') = "+'RAHUL'.split('')+
"\n 'Hello+World'.split('m') = "+'Hello,World'.split('+')
)Searching
IN [217]
// indexOf()
let text = "Please locate where 'locate' occurs!";
console.log(
" text = "+text+
"\n text.indexOf('locate') = "+text.indexOf('locate')+
"\n text.lastIndexOf('locate') = "+text.lastIndexOf('locate')+
"\n text.lastIndexOf('John') = "+text.lastIndexOf('John')+" # -1 if not found"+
"\n text.indexOf('locate', 10) = "+text.indexOf('locate', 10)+" # start from 10th index"
) IN [219]
// search() can't take second parameter starting index thats why indexOf() is better
let text = "Please locate where 'locate' occurs!";
console.log(
" text = "+text+
"\n text.search('locate') = "+text.search('locate')+" # 7th index"+
"\n text.search(/locate/g) = "+text.search(/locate/g)+" # regex"
) IN [2]
// match()
let text = "The rain in SPAIN stays mainly in the plain";
console.log(
" text = "+text+
"\n text.match('ain') = "+text.match('ain')+
"\n text.match(/ain/g) = "+text.match(/ain/g)+" # regex"+
"\n text.match(/ain/gi) = "+text.match(/ain/gi)+" # regex case insensitive"
) IN [3]
// matchAll()
let text = "The rain in SPAIN stays mainly in the plain";
let regex = /ain/g;
let matches = text.matchAll(regex); // returns iterable object
let result = "";
for (const match of matches) {
result += match + "\n";
}
console.log(" text = "+text+"\n result = "+result) IN [4]
// includes()
let text = "Hello World";
console.log(
" text = "+text+
"\n text.includes('World') = "+text.includes('World')+
"\n text.includes('Hello') = "+text.includes('Hello')+" # case sensitive"+
"\n text.includes('hello') = "+text.includes('hello')+" # case insensitive"
) IN [5]
// Template (double tick ``) can access variables called string interpolation
let firstName = "John";
let lastName = "Doe";
let text = `Welcome ${firstName}, ${lastName}!`;
console.log(" text = "+text) IN [ ]