Sure! Here are examples of all the JavaScript string methods with explanations:
charAt(): Returns the character at a specified index in a string.
const str = "Hello";
console.log(str.charAt(0)); // Output: "H"
charCodeAt(): Returns the Unicode value of the character at a specified index in a string.
const str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72
concat(): Concatenates two or more strings and returns a new string.
const str1 = "Hello";
const str2 = "World";
console.log(str1.concat(" ", str2)); // Output: "Hello World"
endsWith(): Checks if a string ends with a specified character or substring. Returns true or false.
const str = "Hello World";
console.log(str.endsWith("World")); // Output: true
includes(): Checks if a string contains a specified character or substring. Returns true or false.
const str = "Hello World";
console.log(str.includes("lo")); // Output: true
indexOf(): Returns the index of the first occurrence of a specified character or substring in a string. Returns -1 if not found.
const str = "Hello World";
console.log(str.indexOf("o")); // Output: 4
lastIndexOf(): Returns the index of the last occurrence of a specified character or substring in a string. Returns -1 if not found.
const str = "Hello World";
console.log(str.lastIndexOf("o")); // Output: 7
length: Returns the length of a string.
const str = "Hello";
console.log(str.length); // Output: 5
match(): Searches a string for a match against a regular expression and returns the matches as an array.
const str = "Hello World";
console.log(str.match(/o/g)); // Output: ["o", "o"]
repeat(): Returns a new string with a specified number of copies of an existing string.
const str = "Hello";
console.log(str.repeat(3)); // Output: "HelloHelloHello"
replace(): Searches a string for a specified value or regular expression and replaces it with a new value.
const str = "Hello World";
console.log(str.replace("World", "Universe")); // Output: "Hello Universe"
search(): Searches a string for a specified value or regular expression and returns the index of the first match.
const str = "Hello World";
console.log(str.search(/o/)); // Output: 4
slice(): Extracts a portion of a string and returns a new string.
const str = "Hello World";
console.log(str.slice(0, 5)); // Output: "Hello"
split(): Splits a string into an array of substrings based on a specified separator.
const str = "Hello, World";
console.log(str.split(",")); // Output: ["Hello", " World"]
startsWith(): Checks if a string starts with a specified character or substring. Returns true or false.
const str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true
substr(): Returns a specified number of characters from a string, starting at a specified index.
const str = "Hello World";
console.log(str.substr(6, 5)); // Output: "World"
toLowerCase(): Converts all characters in a string to lowercase.
const str = "Hello World";
console.log(str.toLowerCase()); // Output: "hello world"
toUpperCase(): Converts all characters in a string to uppercase.
const str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"
trim(): Removes whitespace from both ends of a string.
const str = " Hello World ";
console.log(str.trim()); // Output: "Hello World"
These examples demonstrate the usage and functionality of all the built-in string methods available in JavaScript. Feel free to experiment with these methods and adapt them to your own use cases.