String Methods in JavaScript
String Manipulation Methods
eval()
- Description: Evaluates a string as JavaScript code.
- Example:
eval("3 - 2"); // 1
eval(new String("3 - 2")); // "3 - 2" (no evaluation for string objects)
charAt()
- Description: Returns the character at the specified index.
- Example:
let str = "Hello";
console.log(str.charAt(1)); // "e"
console.log(str[1]); // "e"
codePointAt()
(ES6)
- Description: Returns the Unicode code point value at the specified position.
- Example:
let str = "A";
console.log(str.codePointAt(0)); // 65
String Comparison
- Description: Compare strings using
<
,>
,=
operators. - Example:
"apple" < "banana"; // true
replace()
- Description: Replaces matched substrings with a new string.
- Example:
let str = "Hello world";
console.log(str.replace("world", "JavaScript")); // "Hello JavaScript"
String.fromCharCode()
- Description: Returns a string created from the specified sequence of UTF-16 code units.
- Example:
console.log(String.fromCharCode(65)); // "A"
String.fromCodePoint()
(ES6)
- Description: Returns a string created from the specified sequence of code points.
- Example:
console.log(String.fromCodePoint(9731)); // "☃"
concat()
- Description: Concatenates two or more strings.
- Example:
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // "Hello World"
includes()
(ES6)
- Description: Checks if a string contains a specified substring.
- Example:
let str = "Hello world";
console.log(str.includes("world")); // true
indexOf()
- Description: Returns the index of the first occurrence of a specified value.
- Example:
let str = "Hello world";
console.log(str.indexOf("o")); // 4
lastIndexOf()
- Description: Returns the index of the last occurrence of a specified value.
- Example:
console.log(str.lastIndexOf("o")); // 7
String Matching and Searching
match()
- Description: Matches a string against a regular expression.
- Example:
let str = "Hello world";
console.log(str.match(/o/g)); // ["o", "o"]
matchAll()
(ES6)
- Description: Returns all matches against a regular expression.
- Example:
for (let match of str.matchAll(/o/g)) {
console.log(match);
}
repeat()
(ES6)
- Description: Repeats the string a specified number of times.
- Example:
let str = "Hello";
console.log(str.repeat(3)); // "HelloHelloHello"
search()
- Description: Searches for a match against a regular expression.
- Example:
let str = "Hello world";
console.log(str.search(/world/)); // 6
Substring Methods
split()
- Description: Splits a string into an array of substrings.
- Example:
let str = "a,b,c";
console.log(str.split(",")); // ["a", "b", "c"]
substr()
- Description: Extracts a substring based on a starting position and length.
- Example:
let str = "Hello world";
console.log(str.substr(6, 5)); // "world"
substring()
- Description: Extracts a substring between two indices.
- Example:
console.log(str.substring(0, 5)); // "Hello"
slice()
- Description: Extracts a substring from a string (similar to
substring()
). - Example:
console.log(str.slice(0, 5)); // "Hello"
Case Conversion
toLowerCase()
, toUpperCase()
- Description: Converts string to lowercase or uppercase.
- Example:
let str = "Hello";
console.log(str.toLowerCase()); // "hello"
console.log(str.toUpperCase()); // "HELLO"
toLocaleLowerCase()
, toLocaleUpperCase()
- Description: Locale-sensitive case conversion.
- Example:
console.log(str.toLocaleLowerCase()); // "hello"
console.log(str.toLocaleUpperCase()); // "HELLO"
Utility Methods
toString()
- Description: Returns a string representation of the object.
- Example:
let num = 123;
console.log(num.toString()); // "123"
trim()
- Description: Removes whitespace from both ends of the string.
- Example:
let str = " Hello ";
console.log(str.trim()); // "Hello"
startsWith()
(ES6)
- Description: Checks if a string starts with a specified value.
- Example:
let str = "Hello world";
console.log(str.startsWith("Hello")); // true
endsWith()
(ES6)
- Description: Checks if a string ends with a specified value.
- Example:
console.log(str.endsWith("world")); // true
normalize()
(ES6)
- Description: Returns the Unicode normalization form of the string.
- Example:
let str = "\u1E9B\u0323";
console.log(str.normalize("NFC")); // "\u1E9B\u0323" -> "\u1E9B\u0323"
raw()
(ES6)
- Description: Returns the raw string representation of template literals.
- Example:
let str = String.raw`Hello\nWorld`;
console.log(str); // "Hello\\nWorld"