String methods
String Methods
JavaScript provides several built-in methods for manipulating strings. These methods allow you to perform various operations on strings, such as extracting substrings, searching for specific characters or patterns, converting case, and more. In this guide, we will explore some of the most commonly used string methods in JavaScript, along with their syntax and examples.
1. length
- Syntax: string.length
- Description: 'Length' is not a method but a property in JavaScript. It allows you to retrieve the length of a string, i.e., the number of characters it contains.
- It is a read-only property, so you cannot modify it.
- Example:
const text = "Hello, World!"; const length = text.length; console.log(length); // Output: 13
2. charAt(index)
- Syntax: string.charAt(index)
- Description: Returns the character at the specified index in the string.
- Example:
const str = "Hello, World!"; console.log(str.charAt(0)); // Output: "H" console.log(str.charAt(7)); // Output: "W"
3. charCodeAt(index)
- Syntax: string.charCodeAt(index)
- Description: Returns the Unicode value of the character at the specified index in the string.
- Example:
const str = "Hello"; console.log(str.charCodeAt(0)); // Output: 72 console.log(str.charCodeAt(1)); // Output: 101
4. concat(str1, str2, ..., strN)
- Syntax: string.concat(str1, str2, ..., strN)
- Description: Combines the original string with one or more strings passed as arguments and returns a new string.
- Example:
const str1 = "Hello"; const str2 = "World"; const result = str1.concat(", ", str2, "!"); console.log(result); // Output: "Hello, World!"
5. includes(searchString, position)
- Syntax: string.includes(searchString, position)
- Description: Checks if the string contains the specified substring. Optionally, you can pass the starting position for the search.
- Example:
const str = "Hello, World!"; console.log(str.includes("Hello")); // Output: true console.log(str.includes("world")); // Output: false (case-sensitive)
6. indexOf(searchValue, fromIndex)
- Syntax: string.indexOf(searchValue, fromIndex)
- Description: Returns the index of the first occurrence of the searchValue in the string. Optionally, you can specify a starting index for the search.
- Example:
const str = "Hello, World!"; console.log(str.indexOf("o")); // Output: 4 console.log(str.indexOf("l", 3)); // Output: 3 (starts searching from index 3)
7. lastIndexOf(searchValue, fromIndex)
- Syntax: string.lastIndexOf(searchValue, fromIndex)
- Description: Returns the index of the last occurrence of the searchValue in the string. Optionally, you can specify a starting index for the search.
- Example:
const str = "Hello, World!"; console.log(str.lastIndexOf("o")); // Output: 8 console.log(str.lastIndexOf("l", 3)); // Output: 2 (starts searching from index 3 backwards)
8. startsWith(searchString, position)
- Syntax: string.startsWith(searchString, position)
- Description: Checks if the string starts with the specified substring. Optionally, you can pass the starting position for the check.
- Example:
const str = "Hello, World!"; console.log(str.startsWith("Hello")); // Output: true console.log(str.startsWith("World", 7)); // Output: true (starts checking from index 7)
9. endsWith(searchString, length)
- Syntax: string.endsWith(searchString, length)
- Description: Checks if the string ends with the specified substring. Optionally, you can pass the length of the substring to consider for the check.
- Example:
const str = "Hello, World!"; console.log(str.endsWith("World!")); // Output: true console.log(str.endsWith("Hello", 7)); // Output: true (considers the first 7 characters)
10. slice(startIndex, endIndex)
- Syntax: string.slice(startIndex, endIndex)
- Description: Extracts a portion of the string, starting from the startIndex up to, but not including, the endIndex. Negative indices count from the end of the string.
- Example:
const str = "Hello, World!"; console.log(str.slice(7)); // Output: "World!" console.log(str.slice(0, 5)); // Output: "Hello" console.log(str.slice(-6, -1)); // Output: "World" (extracts from index -6 to -2)
11. substring(startIndex, endIndex)
- Syntax: string.substring(startIndex, endIndex)
- Description: Similar to slice(), extracts a portion of the string from startIndex up to, but not including, the endIndex. However, it does not support negative indices.
- Example:
const str = "Hello, World!"; console.log(str.substring(7)); // Output: "World!" console.log(str.substring(0, 5)); // Output: "Hello"
12. substr(startIndex, length)
- Syntax: string.substr(startIndex, length)
- Description: Extracts a substring from the startIndex of the specified length.
- Example:
const str = "Hello, World!"; console.log(str.substr(7)); // Output: "World!" console.log(str.substr(0, 5)); // Output: "Hello"
13. toLowerCase()
- Syntax: string.toLowerCase()
- Description: Converts the string to lowercase.
- Example:
const str = "Hello, World!"; console.log(str.toLowerCase()); // Output: "hello, world!"
14. toUpperCase()
- Syntax: string.toUpperCase()
- Description: Converts the string to uppercase.
- Example:
const str = "Hello, World!"; console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"
15. trim()
- Syntax: string.trim()
- Description: Removes leading and trailing whitespaces from the string. But it can not remove the spaces inside a string.
- Example:
const str = " Hello, World! "; console.log(str.trim()); // Output: "Hello, World!"
16. split(separator, limit)
- Syntax: string.split(separator, limit)
- Description: Splits the string into an array of substrings based on the specified separator. Optionally, you can specify a limit for the number of splits.
- Example:
const str = "apple,banana,orange"; const fruitsArray = str.split(","); console.log(fruitsArray); // Output: ["apple", "banana", "orange"] const sentence = "JavaScript is awesome and JavaScript is fun."; const wordsArray = sentence.split(" ", 3); console.log(wordsArray); // Output: ["JavaScript", "is", "awesome"]
17. replace(searchValue, replaceValue)
- Syntax: string.replace(searchValue, replaceValue)
- Description: The `replace` method replaces a specified substring or pattern with another substring.
- Example:
const text = "Hello, World!"; const newText = text.replace("World", "JavaScript"); console.log(newText); // Output: Hello, JavaScript!
Key Takeaways
- charAt(index): Returns the character at the specified index in the string.
- charCodeAt(index): Returns the Unicode value of the character at the specified index in the string.
- concat(str1, str2, ..., strN): Combines the original string with one or more strings passed as arguments and returns a new string.
- includes(searchString, position): Checks if the string contains the specified substring. Optionally, you can pass the starting position for the search.
- indexOf(searchValue, fromIndex): Returns the index of the first occurrence of the searchValue in the string. Optionally, you can specify a starting index for the search.
- lastIndexOf(searchValue, fromIndex): Returns the index of the last occurrence of the searchValue in the string. Optionally, you can specify a starting index for the search.
- startsWith(searchString, position): Checks if the string starts with the specified substring. Optionally, you can pass the starting position for the check.
- endsWith(searchString, length): Checks if the string ends with the specified substring. Optionally, you can pass the length of the substring to consider for the check.
- slice(startIndex, endIndex): Extracts a portion of the string, starting from the startIndex up to, but not including, the endIndex.
- substring(startIndex, endIndex): Similar to slice(), extracts a portion of the string from startIndex up to, but not including, the endIndex. Does not support negative indices.
- substr(startIndex, length): Extracts a substring from the startIndex of the specified length.
- toLowerCase(): Converts the string to lowercase.
- toUpperCase(): Converts the string to uppercase.
- trim(): Removes leading and trailing whitespaces from the string.
- split(separator, limit): Splits the string into an array of substrings based on the specified separator. Optionally, you can specify a limit for the number of splits.
- string.length: Returns the number of characters in the string.
Comments
Post a Comment