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