JavaScript strings are primitive values. JavaScript strings are also immutable. It means that if you process a string, you will always get a new string. The original string doesn’t change. Strings are used for storing and manipulating text.
To create a string, you can use single quote or double quote.
let firstName = 'Folau';
let lastName = "Kaveinga";String as objects
Don’t create strings as objects. It slows down execution speed and when it comes to comparison, it produces unexpected results.
let folau = new String("Folau");
let lisa = new String("Lisa");
let sameString = (folau=="Folau");// true
let sameObject = (folau==="Folau");// false
//When using the === operator, equal strings are not equal, because the === operator expects equality in both type and value.
var kinga = new String("kinga");
var king = new String("kinga");
let samePerson = (kinga==king);// false, even though the value for both objects is "kinga"
To escape special characters, you use the backslash \ character.
if it’s a single quote string and you want to escape a single quote, you do this: \’
if it’s a double quote string and you want to escape a double quote, you do this: \”
if want to escape a backslash \, you do this: \\
let str = 'I\'m Folau Kaveinga.';
document.getElementById("userDesc").innerHTML = str;
let str1 = "I\"m Folau Kaveinga.";
let str2 = 'I\'m slash \\';
document.getElementById("userDesc").innerHTML += str1;
document.getElementById("userDesc").innerHTML += str2;
// output
I'm Folau Kaveinga.I"m Folau Kaveinga.I'm slash \
To get length of a string, you use the length function
let str = 'I\'m Folau Kaveinga.';
let strLength = str.length;
console.log("strLength: "+strLength); // strLength: 19To access the characters in a string, you use the array-like [] notation with the zero-based index.
let name = "Folau Kaveinga";
let letter = name[4];
console.log("letter: "+letter);// uTo convert a non-string value to a string, use toString(), number + “”, or String(number)
let active = false;
let activeStr = active.toString();
console.log("activeStr: "+activeStr);// "false"let five = 5;
let fiveStr = new String(five);
console.log("fiveStr: "+fiveStr);// 5String concatenate
The concat() method concatenates one or more strings to another and returns the result string. Note that the concat() method does not modify the original string.
let firstName = 'Folau';
let fullName = firstName.concat(' ','Kaveinga', ' ', 'Jr');
console.log(fullName); // "Folau Kaveinga Jr"You can also use the + operator to concatenate strings. In practice, the addition operator is used more often than the concate() method.
let firstName = 'Folau';
let fullName = firstName + ' '+ 'Kaveinga'+' '+'Jr';
console.log(fullName); // "Folau Kaveinga Jr"To get a substring, use the substr or substring function
substr(startIndex,length);The first argument startIndex the location at which the characters are being extracted, while the second argument length specifies the number of characters to extract.
substring(startIndex,endIndex)Sometimes, you want to extract a substring from a string using starting and ending indexes.
let phrase = "I love coding";
console.log(phrase.substr(2, 4)); // "love"
console.log(phrase.substr(7, 6)); // "coding"
console.log(phrase.substring(7, 13)); // "coding"To find position of a substring with a string, use the indexOf function
string.indexOf(substring,fromIndex);You can also find the last index of a substring in a string by using the lastIndexOf function
string.lastIndexOf(substring,fromIndex)let phrase = "I love coding so much";
console.log(phrase.indexOf("o")); // 3
console.log(phrase.indexOf('o', 3)); // 3
console.log(phrase.lastIndexOf('o')); // 15
console.log(phrase.indexOf('test'));// -1The indexOf and lastIndexOf functions return -1 if substring is not found in the string.
There are more modern methods str.includes(substr, pos) returns true/false depending on whether str contains subtr within.
console.log( "Folau".includes("lau") ); // true
console.log( "Folau".includes("ol", 4 )); // false, from position 4 there is no "ol"
console.log( "Folau".startsWith("Fol") ); // true, "Folau" starts with "Fol"
console.log( "Folau".endsWith("lau") ); // true, "Folau" ends with "lau"
Remove leading and trailing whitespace, use trim(), trimStart(), and trimEnd()
let name = ' Folau ';
let strippedName= name.trim();
let leadingStrippedName= name.trimStart();
let trailingStrippedName= name.trimEnd();
console.log(strippedName); // "Folau"
console.log(leadingStrippedName); // "Folau "
console.log(trailingStrippedName); // " Folau"Replace substring within a string, use replace function
string.replace(regularExpression, replaceText)The first argument of the replace method could be a regular expression or a string. If it is a regular expression, the replace() method will find the matches and replace them with the second argument (replaceText). In case it is a string, the replace() method will perform an exact search and carry the replacement.
let phrase = "I love coding so much";
// replace "the" with "a"
let newPhrase = phrase.replace(/so/g, "very");
console.log(newPhrase); // "I love coding very much"
console.log(phrase); // "I love coding so much"