All String Method in Javascript
Show all function: Try
String.prototype
&String
in browser console.
fromCharCode
console.log(String.fromCharCode(189, 43, 190, 61));
// expected output: "½+¾="
fromCodePoint
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2f804));
// expected output: "☃★♲你"
raw
// Create a variable that uses a Windows
// path without escaping the backslashes:
const filePath = String.raw`C:\Development\profile\aboutme.html`;
console.log(`The file was uploaded from: ${filePath}`);
// expected output: "The file was uploaded from: C:\Development\profile\aboutme.html"
charAt
const sentence = 'The quick brown fox jumps over the lazy dog.';
const index = 4;
console.log(`The character at index ${index} is ${sentence.charAt(index)}`);
// expected output: "The character at index 4 is q"
charCodeAt
const sentence = 'The quick brown fox jumps over the lazy dog.';
const index = 4;
console.log(
`The character code ${sentence.charCodeAt(
index
)} is equal to ${sentence.charAt(index)}`
);
// expected output: "The character code 113 is equal to q"
codePointAt
const icons = '☃★♲';
console.log(icons.codePointAt(1));
// expected output: "9733"
concat
const str1 = 'Hello';
const str2 = 'World';
console.log(str1.concat(' ', str2));
// expected output: "Hello World"
console.log(str2.concat(', ', str1));
// expected output: "World, Hello"
startsWith
const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));
// expected output: true
console.log(str1.startsWith('Sat', 3));
// expected output: false
console.log(str1.startsWith('ur', 3));
// true
endsWith
const str1 = 'Cats are the best!'; // len == 18
console.log(str1.endsWith('best', 17));
// expected output: true
const str2 = 'Is this a question';
console.log(str2.endsWith('?'));
// expected output: false
includes
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"
indexOf
const paragraph =
'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(
`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`
);
// expected output: "The index of the first "dog" from the beginning is 40"
console.log(
`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(
searchTerm,
indexOfFirst + 1
)}`
);
// expected output: "The index of the 2nd "dog" is 52"
lastIndexOf
const paragraph =
'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const searchTerm = 'dog';
console.log(
`The index of the first "${searchTerm}" from the end is ${paragraph.lastIndexOf(
searchTerm
)}`
);
// expected output: "The index of the first "dog" from the end is 52"
localeCompare
const a = 'réservé'; // with accents, lowercase
const b = 'RESERVE'; // no accents, uppercase
console.log(a.localeCompare(b));
// expected output: 1
console.log(a.localeCompare(b, 'en', { sensitivity: 'base' }));
// expected output: 0
match
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"]
matchAll
The matchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups.