String.
length
Syntax: string.length
Explanation: Returns the number of characters in the string.
Example:
let text = "Hello";
console.log(text.length); // 5
String.charAt()
Syntax: string.charAt(index)
Explanation: Returns the character at the specified position.
Example:
let text = "Hello";
console.log(text.charAt(1)); // "e"
String.charCodeAt()
Syntax: string.charCodeAt(index)
Explanation: Returns the Unicode of the character at the specified index.
Example:
let text = "A";
console.log(text.charCodeAt(0)); // 65
String.at()
Syntax: string.at(index)
Explanation: Returns the character at a specified index, supports negative indexing.
Example:
let text = "Hello";
console.log(text.at(-1)); // "o"
String[index]
Syntax: string[index]
Explanation: Returns the character at the given index (like an array).
Example:
let text = "Hello";
console.log(text[0]); // "H"
String.slice()
Syntax: string.slice(start, end)
Explanation: Extracts part of a string and returns it as a new string.
Example:
let text = "Hello World";
console.log(text.slice(0, 5)); // "Hello"
String.substring()
Syntax: string.substring(start, end)
Explanation: Similar to slice(), but doesn't accept negative values.
Example:
let text = "Hello World";
console.log(text.substring(0, 5)); // "Hello"
String.substr()
Syntax: string.substr(start, length)
Explanation: Returns part of a string starting at a position and length.
Example:
let text = "Hello World";
console.log(text.substr(6, 5)); // "World"
String.toUpperCase()
Syntax: string.toUpperCase()
Explanation: Converts all characters to uppercase.
Example:
let text = "hello";
console.log(text.toUpperCase()); // "HELLO"
String.toLowerCase()
Syntax: string.toLowerCase()
Explanation: Converts all characters to lowercase.
Example:
let text = "HELLO";
console.log(text.toLowerCase()); // "hello"
String.concat()
Syntax: string1.concat(string2)
Explanation: Joins two or more strings.
Example:
let a = "Hello";
let b = "World";
console.log(a.concat(" ", b)); // "Hello World"
String.trim()
Syntax: string.trim()
Explanation: Removes whitespace from both ends.
Example:
let text = " Hello ";
console.log(text.trim()); // "Hello"
String.trimStart()
Syntax: string.trimStart()
Explanation: Removes whitespace from the beginning.
Example:
let text = " Hello";
console.log(text.trimStart()); // "Hello"
String.trimEnd()
Syntax: string.trimEnd()
Explanation: Removes whitespace from the end.
Example:
let text = "Hello ";
console.log(text.trimEnd()); // "Hello"
String.padStart()
Syntax: string.padStart(length, padString)
Explanation: Pads the string from the start until it reaches the specified length.
Example:
let text = "5";
console.log(text.padStart(3, "0")); // "005"
String.padEnd()
Syntax: string.padEnd(length, padString)
Explanation: Pads the string at the end.
Example:
let text = "5";
console.log(text.padEnd(3, "0")); // "500"
String.repeat()
Syntax: string.repeat(count)
Explanation: Returns the string repeated count times.
Example:
let text = "Hi";
console.log(text.repeat(3)); // "HiHiHi"
String.replace()
Syntax: string.replace(search, replacement)
Explanation: Replaces the first match of a substring or pattern.
Example:
let text = "apple banana apple";
console.log(text.replace("apple", "orange")); // "orange banana apple"
String.replaceAll()
Syntax: string.replaceAll(search, replacement)
Explanation: Replaces all occurrences of a substring or pattern.
Example:
let text = "apple banana apple";
console.log(text.replaceAll("apple", "orange")); // "orange banana orange"
String.split()
Syntax: string.split(separator)
Explanation: Splits the string into an array of substrings.
Example:
let text = "a,b,c";
console.log(text.split(",")); // ["a", "b", "c"]