Strings

The String object is used to represent and manipulate a sequence of characters. Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.

const string1 = "A string primitive";
const string2 = 'Also a string primitive';
const string3 = `Yet another string primitive`;
const string4 = new String("A String object");

template literal or string interpolation

const rollNo16 = "Anubhav Tiwari";
const rollNo05 = "Aditya Sir";

const result = `${rollNo05} comes before ${rollNo16}`;
console.log(result);    // Aditya Sir comes before Anubhav Tiwari

uppercase and lowercase

console.log(rollNo16.toUpperCase());    // toUpperCase -> ANUBHAV TIWARI
console.log(rollNo05.toLowerCase());    // toLowerCase -> aditya sir

slice()

the slice() method extracts a section of the string and returns it as a new string, without modifying the original string.

console.log(rollNo05.slice(2, 4));   // it
console.log(rollNo05.slice());  // itya Sir

replace() and replaceAll()

The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.

The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.

console.log(rollNo05.replace("Sir", "Gautam"));  // Aditya Gautam

const str = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
console.log(str.replaceAll("the", "THE"));
// The quick brown fox jumps over THE lazy dog. If THE dog reacted, was it really lazy?

console.log(str.replaceAll("dog", "DOG"));
// The quick brown fox jumps over the lazy DOG. If the DOG reacted, was it really lazy?

concat()

The concat() method of String values concatenates the string arguments to this string and returns a new string.

console.log(rollNo05.concat(rollNo16));
// Aditya SirAnubhav Tiwari

trim(), trimEnd() and trimStart()

The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string. To return a new string with whitespace trimmed from just one end, use trimStart() or trimEnd().

The trimEnd() method of String values removes whitespace from the end of this string and returns a new string, without modifying the original string. trimRight() is an alias of this method.

The trimStart() method of String values removes whitespace from the beginning of this string and returns a new string, without modifying the original string. trimLeft() is an alias of this method.

const greeting = '   Hello world!   ';

console.log(greeting);
// Expected output: "   Hello world!   ";
                    
console.log(greeting.trim());
// Expected output: "Hello world!";

console.log(greeting.trimEnd());
// Expected output: "   Hello world!";

console.log(greeting.trimStart());
// Expected output: "Hello world!   ";

for...in loop

// for...in loop accessing character from string
for (let key in rollNo05) {
    console.log(rollNo05[key]);
}

at()

The at() method of String values takes an integer value and returns a new String consisting of the single UTF-16 code unit located at the specified offset. This method allows for positive and negative integers. Negative integers count back from the last string character.

at => use at instead of charAt()
let index1 = 5;
let index2 = -5
console.log(`character at index 5 and -5 of "Aditya Sir" are  ${rollNo05.at(index1)} and ${rollNo05.at(index2)} respectively.`);
//character at index 5 and -5 of "Aditya Sir" are a and a respectively.

endsWith()

The endsWith() method of String values determines whether a string ends with the characters of this string, returning true or false as appropriate.

console.log(rollNo16.endsWith("Tiwari"));
console.log(rollNo16.endsWith("ari"));
console.log(rollNo16.endsWith("ar", 13));
console.log(rollNo16.endsWith("wa", 13));

startsWith()

The startsWith() method of String values determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

console.log(rollNo16.endsWith("Tiwari"));
console.log(rollNo16.endsWith("ari"));
console.log(rollNo16.endsWith("ar", 13));
console.log(rollNo16.endsWith("wa", 13));

repeat()

The repeat() method of String values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.

const word="happy! ";
console.log(`i am ${word.repeat(3)}`);  // i am happy! happy! happy!

valueOf()

const str = new String("fruit");
console.log(str);   // [String: 'fruit']
console.log(str.valueOf()); // fruit

split()

The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str);

const strCopy = str.split();
console.log(typeof strCopy);    // object
console.log(strCopy);
// Expected output: Array ["The quick brown fox jumps over the lazy dog."]

const chars = str.split('');
console.log(typeof str.split(''));  // object
console.log(chars[8]);
// Expected output: "k"

const words = str.split(' ');
console.log(typeof str.split(' '));     // object
console.log(words[3]);
// Expected output: "fox"

includes()

The includes() method of String values performs a case-sensitive search to determine whether a given string may be found within this string, returning true or false as appropriate.

const word1 = "ari";
console.log(`the word "${word1}" ${rollNo16.includes(word1) ? 'is' : 'is not'} in the ${rollNo16}`);    // is

const word2 = "vT";
console.log(`the word "${word2}" ${rollNo16.includes(word2) ? 'is' : 'is not'} in the ${rollNo16}`);    // is not