JavaScript string methods are powerful tools that allow developers to manipulate and handle text efficiently. These methods come in handy for tasks such as searching, extracting, and modifying string content. Here, we’ll explore some of the most commonly used JavaScript string methods.
1. charAt():
The charAt() method returns the character at a specified index (position) in a string.
Syntax:
string.charAt(index);
Example:
let text = "Hello, World!";
console.log(text.charAt(0)); // Outputs: H
2. charCodeAt();
The charCodeAt() method returns the Unicode of the character at a specified index in a string.
Syntax:
string.charCodeAt(index);
Example:
let text = "Hello, World!";
console.log(text.charCodeAt(0)); // Outputs: 72
3. concat():
The concat() method joins two or more strings and returns a new string.
Syntax:
string1.concat(string2, string3, ..., stringN);
Example:
let text1 = "Hello";
let text2 = "World";
let result = text1.concat(", ", text2, "!");
console.log(result); // Outputs: Hello, World!
4. includes():
The includes() method checks if a string contains a specified substring and returns true or false.
Syntax:
string.includes(searchString, position);
Example:
let text = "Hello, World!";
console.log(text.includes("World")); // Outputs: true
5. indexOf():
The indexOf() method returns the position of the first occurrence of a specified value in a string. It returns -1 if the value is not found.
Syntax:
string.indexOf(searchValue, fromIndex);
Example:
let text = "Hello, World!";
console.log(text.indexOf("World")); // Outputs: 7
6. lastIndexOf():
The lastIndexOf() method returns the position of the last occurrence of a specified value in a string. It returns -1 if the value is not found.
Syntax:
string.lastIndexOf(searchValue, fromIndex);
Example:
let text = "Hello, World! Hello, JavaScript!";
console.log(text.lastIndexOf("Hello")); // Outputs: 14
7. replace():
The replace() method searches a string for a specified value or a regular expression and returns a new string with the specified values replaced.
Syntax:
string.replace(searchValue, newValue);
Example:
let text = "Hello, World!";
let newText = text.replace("World", "JavaScript");
console.log(newText); // Outputs: Hello, JavaScript!
8. slice():
The slice() method extracts a part of a string and returns it as a new string. It takes two arguments: the start index and the end index (optional).
Syntax:
string.slice(startIndex, endIndex);
Example:
let text = "Hello, World!";
let slicedText = text.slice(0, 5);
console.log(slicedText); // Outputs: Hello
9. split():
The split() method splits a string into an array of substrings based on a specified separator.
Syntax:
string.split(separator, limit);
Example:
let text = "Hello, World!";
let words = text.split(" ");
console.log(words); // Outputs: ["Hello,", "World!"]
10. substring():
The substring() method extracts characters from a string, between two specified indices, and returns the new substring.
Syntax:
string.substring(startIndex, endIndex);
Example:
let text = "Hello, World!";
let subText = text.substring(0, 5);
console.log(subText); // Outputs: Hello
11. toLowerCase():
The toLowerCase() method converts a string to lowercase letters.
Syntax:
string.toLowerCase();
Example:
let text = "Hello, World!";
let lowerText = text.toLowerCase();
console.log(lowerText); // Outputs: hello, world!
12. toUpperCase():
The toUpperCase() method converts a string to uppercase letters.
Syntax:
string.toUpperCase();
Example:
let text = "Hello, World!";
let upperText = text.toUpperCase();
console.log(upperText); // Outputs: HELLO, WORLD!
13. trim():
The trim() method removes whitespace from both ends of a string.
Syntax:
string.trim();
Example:
let textWithWhitespace = " Hello, World! ";
let trimmedText = textWithWhitespace.trim();
console.log(trimmedText); // Outputs: Hello, World!
14. startsWith():
The startsWith() method checks if a string begins with specified characters and returns true or false.
Syntax:
string.startsWith(searchString, position);
Example:
let text = "Hello, World!";
console.log(text.startsWith("Hello")); // Outputs: true
15. endsWith():
The endsWith() method checks if a string ends with specified characters and returns true or false.
Syntax:
string.endsWith(searchString, length);
Example:
let text = "Hello, World!";
console.log(text.endsWith("World!")); // Outputs: true
Conclusion:
JavaScript string methods are essential for handling and manipulating text in web applications. By mastering these methods, you can efficiently perform a wide range of text operations, from searching and extracting to transforming and formatting strings. This knowledge is fundamental for creating dynamic and interactive web content.