JavaScript String Methods
charAt()
Description: Returns the character at a specified index.
Example:
let str = "Hello World";
console.log(str.charAt(6)); // Output: W
charCodeAt()
Description: Returns the Unicode of the character at a specified index.
Example:
let str = "Hello World";
console.log(str.charCodeAt(6)); // Output: 87
concat()
Description: Joins two or more strings.
Example:
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: Hello World
includes()
Description: Checks if a string contains a specified value.
Example:
let str = "Hello World";
console.log(str.includes("World")); // Output: true
endsWith()
Description: Checks if a string ends with a specified value.
Example:
let str = "Hello World";
console.log(str.endsWith("World")); // Output: true
indexOf()
Description: Returns the position of the first occurrence of a specified value.
Example:
let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6
lastIndexOf()
Description: Returns the position of the last occurrence of a specified value.
Example:
let str = "Hello World, Hello Universe";
console.log(str.lastIndexOf("Hello")); // Output: 13
localeCompare()
Description: Compares two strings in the current locale.
Example:
let str1 = "apple";
let str2 = "banana";
console.log(str1.localeCompare(str2)); // Output: -1 (str1 comes before str2)
match()
Description: Matches a string against a regular expression.
Example:
let str = "Hello World";
console.log(str.match(/World/)); // Output: ["World"]
matchAll()
Description: Returns all matches of a string against a regular expression.
Example:
let str = "test1 test2 test3";
let regex = /test\d/g;
console.log([...str.matchAll(regex)]); // Output: [["test1"], ["test2"],
["test3"]]
padStart()
Description: Pads the string with a specified character at the start.
Example:
let str = "5";
console.log(str.padStart(3, "0")); // Output: 005
padEnd()
Description: Pads the string with a specified character at the end.
Example:
let str = "5";
console.log(str.padEnd(3, "0")); // Output: 500
repeat()
Description: Repeats a string a specified number of times.
Example:
let str = "Hello ";
console.log(str.repeat(3)); // Output: Hello Hello Hello
replace()
Description: Replaces a specified value with another value.
Example:
let str = "Hello World";
console.log(str.replace("World", "Universe")); // Output: Hello Universe
replaceAll()
Description: Replaces all occurrences of a specified value.
Example:
let str = "apple apple apple";
console.log(str.replaceAll("apple", "banana")); // Output: banana banana banana
slice()
Description: Extracts a part of a string and returns it as a new string.
Example:
let str = "Hello World";
console.log(str.slice(6, 11)); // Output: World
split()
Description: Splits a string into an array of substrings.
Example:
let str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]
startsWith()
Description: Checks if a string starts with a specified value.
Example:
let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true
substring()
Description: Extracts characters between two indices.
Example:
let str = "Hello World";
console.log(str.substring(6, 11)); // Output: World
toLowerCase()
Description: Converts a string to lowercase.
Example:
let str = "Hello World";
console.log(str.toLowerCase()); // Output: hello world
toUpperCase()
Description: Converts a string to uppercase.
Example:
let str = "Hello World";
console.log(str.toUpperCase()); // Output: HELLO WORLD
trim()
Description: Removes whitespace from both ends of a string.
Example:
let str = " Hello World ";
console.log(str.trim()); // Output: Hello World
trimStart()
Description: Removes whitespace from the beginning of a string.
Example:
let str = " Hello World";
console.log(str.trimStart()); // Output: Hello World
trimEnd()
Description: Removes whitespace from the end of a string.
Example:
let str = "Hello World ";
console.log(str.trimEnd()); // Output: Hello World
valueOf()
Description: Returns the primitive value of a string.
Example:
let str = new String("Hello World");
console.log(str.valueOf()); // Output: Hello World