Reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

 

string methods

 

• substring(startIndex, endIndex) - returns a substring... not including the endIndex

• slice(start, end)

Both are similar. 

 

const text = "Mozilla"

text.substring(5,2)  // zil

text.slice(5,2)         // ""
console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
 

• replace( pattern, replacement )

const paragraph = "I think Ruth's dog is cuter than your dog!";

console.log(paragraph.replace("Ruth's", 'my'));
// Expected output: "I think my dog is cuter than your dog!"

const regex = /Dog/i;
console.log(paragraph.replace(regex, 'ferret'));
// Expected output: "I think Ruth's ferret is cuter than your dog!"

 

• search(regex)

const paragraph = "I think Ruth's dog is cuter than your dog!";

// Anything not a word character, whitespace or apostrophe
const regex = /[^\w\s']/g;

console.log(paragraph.search(regex));
// Expected output: 41

console.log(paragraph[paragraph.search(regex)]);
// Expected output: "!"

 

• includes(searchString)

• includes(searchString, position)

const sentence = 'The quick brown fox jumps over the lazy dog.';

const word = 'fox';

console.log(
  `The word "${word}" ${
    sentence.includes(word) ? 'is' : 'is not'
  } in the sentence`,
);
// Expected output: "The word "fox" is in the sentence"

 

• slice(indexStart)

• slice(indexStart, indexEnd)

const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// Expected output: "the lazy dog."

console.log(str.slice(4, 19));
// Expected output: "quick brown fox"

console.log(str.slice(-4));
// Expected output: "dog."

console.log(str.slice(-9, -5));
// Expected output: "lazy"

 

• match(regexp)

const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);

console.log(found);
// Expected output: Array ["T", "I"]

 

 

Easy methods

• lastIndexOf('substring') 

• charCodeAt(index)

const sentence = 'The quick brown fox jumps over the lazy dog.';

const index = 4;

console.log(
  `Character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(
    index,
  )}`,
);
// Expected output: "Character code 113 is equal to q"

 

• concat()

• lastIndexOf('')

console.log(myString.lastIndexOf(''))

 

• toLocalLowerCase()

• toLocalUpperCase()

• indexOf()

 

• trim() - removes blank space at the start and the end

• charAt()

 

• split(separator, limitOfItemsReturned) 

"1,2,3,4,5,6".split(',')