JavaScript provides a variety of methods to work with strings. Here are some commonly used string methods:
1. length
The length
property returns the length of a string:
var str = "Hello, world!";
console.log(str.length); // Output: 13
2. charAt()
The charAt()
method returns the character at a specified index (position) in a string:
var str = "Hello, world!";
console.log(str.charAt(0)); // Output: H
3. concat()
The concat()
method joins two or more strings:
var str1 = "Hello, ";
var str2 = "world!";
console.log(str1.concat(str2)); // Output: Hello, world!
4. indexOf()
The indexOf()
method returns the index of the first occurrence of a specified value in a string:
var str = "Hello, world!";
console.log(str.indexOf("world")); // Output: 7
5. lastIndexOf()
The lastIndexOf()
method returns the index of the last occurrence of a specified value in a string:
var str = "Hello, world! Hello, world!";
console.log(str.lastIndexOf("world")); // Output: 19
6. slice()
The slice()
method extracts a section of a string and returns it as a new string:
var str = "Hello, world!";
console.log(str.slice(7)); // Output: world!
7. substring()
The substring()
method extracts the characters from a string, between two specified indices, and returns the new sub-string:
var str = "Hello, world!";
console.log(str.substring(7, 12)); // Output: world
8. substr()
The substr()
method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters:
var str = "Hello, world!";
console.log(str.substr(7, 5)); // Output: world
9. replace()
The replace()
method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced:
var str = "Visit Microsoft!";
var newStr = str.replace("Microsoft", "Google");
console.log(newStr); // Output: Visit Google!
10. split()
The split()
method splits a string into an array of substrings based on a specified separator:
var str = "Hello, world!";
console.log(str.split(", ")); // Output: ["Hello", "world!"]
11. toUpperCase() / toLowerCase()
The toUpperCase()
method converts a string to uppercase, while the toLowerCase()
method converts a string to lowercase:
var str = "Hello, world!";
console.log(str.toUpperCase()); // Output: HELLO, WORLD!
console.log(str.toLowerCase()); // Output: hello, world!
12. trim()
The trim()
method removes whitespace from both ends of a string:
var str = " Hello, world! ";
console.log(str.trim()); // Output: Hello, world!
13. startsWith()
The startsWith()
method determines whether a string begins with the characters of a specified string:
var str = "Hello, world!";
console.log(str.startsWith("Hello")); // Output: true
14. endsWith()
The endsWith()
method determines whether a string ends with the characters of a specified string:
var str = "Hello, world!";
console.log(str.endsWith("world!")); // Output: true
15. includes()
The includes()
method determines whether a string contains the specified characters:
var str = "Hello, world!";
console.log(str.includes("world")); // Output: true