String Methods

In JavaScript, strings come with a rich set of methods that allow you to manipulate and interact with them in various ways. Here are some of the most common methods:

1. Using charAt()

Description: Returns the character at the specified index (position).

Syntax: string.charAt(index)

Example:

let str = "Hello";
console.log(str.charAt(0)); // Output: H

2. Using charCodeAt()

Description: Returns the Unicode of the character at the specified index.

Syntax: string.charCodeAt(index)

Example:

let str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72

3. Using concat()

Description: Joins two or more strings and returns a new joined string.

Syntax: string.concat(string1, string2, ...)

Example:

let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: Hello World

4. Using includes()

Description: Checks whether a string contains the specified string/characters.

Syntax: string.includes(searchString)

Example:

let str = "Hello World";
console.log(str.includes("World")); // Output: true

5. Using endsWith()

Description: Checks whether a string ends with specified string/characters.

Syntax: string.endsWith(searchString)

Example:

let str = "Hello World";
console.log(str.endsWith("World")); // Output: true

6. Using indexOf()

Description: Returns the index of the first occurrence of a specified value in a string.

Syntax: string.indexOf(searchValue)

Example:

let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6

7. Using lastIndexOf()

Description: Returns the index of the last occurrence of a specified value in a string.

Syntax: string.lastIndexOf(searchValue)

Example:

let str = "Hello World World";
console.log(str.lastIndexOf("World")); // Output: 12

8. Using match()

Description: Searches a string for a match against a regular expression, and returns the matches.

Syntax: string.match(regexp)

Example:

let str = "Hello World";
console.log(str.match(/World/g)); // Output: ["World"]

9. Using matchAll()

Description: Returns an iterator of all matches of a string against a regular expression.

Syntax: string.matchAll(regexp)

Example:

let str = "Hello World World";
const matches = str.matchAll(/World/g);
for (const match of matches) {
    console.log(match[0]); // Output: World
}

10. Using normalize()

Description: Returns the Unicode Normalization Form of the string.

Syntax: string.normalize(form)

Example:

let str = "é";
console.log(str.normalize("NFD")); // Output: é

11. Using padEnd()

Description: Pads the current string with another string until the resulting string reaches the given length.

Syntax: string.padEnd(length, padString)

Example:

let str = "Hello";
console.log(str.padEnd(10, ".")); // Output: Hello.....

12. Using padStart()

Description: Pads the current string with another string until the resulting string reaches the given length.

Syntax: string.padStart(length, padString)

Example:

let str = "Hello";
console.log(str.padStart(10, ".")); // Output: .....Hello

13. Using repeat()

Description: Returns a new string with a specified number of copies of the string.

Syntax: string.repeat(count)

Example:

let str = "Hello";
console.log(str.repeat(3)); // Output: HelloHelloHello

14. Using replace()

Description: Searches a string for a specified value, and returns a new string where the specified values are replaced.

Syntax: string.replace(searchValue, newValue)

Example:

let str = "Hello World";
console.log(str.replace("World", "Everyone")); // Output: Hello Everyone

15. Using replaceAll()

Description: Returns a new string with all matches of a pattern replaced by a replacement.

Syntax: string.replaceAll(searchValue, newValue)

Example:

let str = "Hello World World";
console.log(str.replaceAll("World", "Everyone")); // Output: Hello Everyone Everyone

16. Using search()

Description: Searches a string for a specified value and returns the position of the match.

Syntax: string.search(searchValue)

Example:

let str = "Hello World";
console.log(str.search("World")); // Output: 6

17. Using slice()

Description: Extracts a part of a string and returns a new string.

Syntax: string.slice(startIndex, endIndex)

Example:

let str = "Hello World";
console.log(str.slice(0, 5)); // Output: Hello

18. Using split()

Description: Splits a string into an array of substrings.

Syntax: string.split(separator)

Example:

let str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]

19. Using startsWith()

Description: Checks whether a string begins with the characters of a specified string.

Syntax: string.startsWith(searchString)

Example:

let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true

20. Using substring()

Description: Returns the part of the string between two specified indices.

Syntax: string.substring(startIndex, endIndex)

Example:

let str = "Hello World";
console.log(str.substring(0, 5)); // Output: Hello

21. Using toLowerCase()

Description: Returns the calling string value converted to lowercase.

Syntax: string.toLowerCase()

Example:

let str = "Hello World";
console.log(str.toLowerCase()); // Output: hello world

22. Using toUpperCase()

Description: Returns the calling string value converted to uppercase.

Syntax: string.toUpperCase()

Example:

let str = "Hello World";
console.log(str.toUpperCase()); // Output: HELLO WORLD

23. Using trim()

Description: Removes whitespace from both ends of a string.

Syntax: string.trim()

Example:

let str = "   Hello World   ";
console.log(str.trim()); // Output: Hello World

24. Using trimEnd()

Description: Removes whitespace from the end of a string.

Syntax: string.trimEnd()

Example:

let str = "   Hello World   ";
console.log(str.trimEnd()); // Output: "   Hello World"

25. Using trimStart()

Description: Removes whitespace from the beginning of a string.

Syntax: string.trimStart()

Example:

let str = "   Hello World   ";
console.log(str.trimStart()); // Output: "Hello World   "

26. Using valueOf()

Description: Returns the primitive value of a String object.

Syntax: string.valueOf()

Example:

let str = new String("Hello");
console.log(str.valueOf()); // Output: Hello