JavaScript string method
String method
let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//length property
console.log(txt.length);
//extracting string parts -> slice(start,end) substr(start,length) substring(start,end)
console.log(txt.slice(3,9))
console.log(txt.substr(6,24))
console.log(txt.substring(8,17)) //17 not included
console.log("negative len",txt.slice(-13,-2)) //pos counted from end
console.log(txt.slice(-3))
console.log(txt.slice(9))
let str = "Apple, Banana, Kiwi";
console.log(str.substr(-4));
//replace
console.log(str.replace("Apple", "mango"))
let text = "Please visit Microsoft and Microsoft!";
console.log(text.replace("Microsoft", "Google")) //only replaces first relevance
console.log(text.replace("MICROSOFT", "mango")) //CASE SENSITVE
console.log(text.replace(/MICROSOFT/i, "mango")) //case insensitive
console.log(text.replace(/Microsoft/g, "Goa")) //replace all strings named "Microsoft"
//uppercase
console.log(text.toUpperCase())
//lower case
console.log(txt.toLowerCase())
//concat() method
console.log(str.concat(" ", text))
/*All string methods return a new string. They don't modify the original string.
Formally said:
Strings are immutable: Strings cannot be changed, only replaced. */
//.trim()
var sentence = " hey why this white space ? "
console.log(sentence.trim())
console.log(sentence.trimStart()) // removes whitespace only from start
console.log(sentence.trimEnd()) // removes whitespace only from end
//padding string methods
let text1 = "5";
text1.toString(); //covert num to str
console.log(text1.padStart(4,"x"))
let text2 = "5";
let padded = text2.padEnd(4,"0");
console.log(padded)