Different string methods in JavaScript
JavaScript provides a wide range of string methods that allow you to manipulate and work with strings in various ways. Here are some of the most commonly used string methods in JavaScript:
1. charAt(index): Returns the character at the specified index.
Example: "hello".charAt(0) === "h"
2. charCodeAt(index): Returns the Unicode code point of the character at the specified index.
Example: "hello".charCodeAt(0) === 104
3. concat(string2, ..., stringN): Concatenates two or more strings.
Example: "hello".concat(" ", "world") === "hello world"
4. endsWith(searchString, [length]): Returns a boolean indicating whether the string ends with the specified searchString.
Example: "hello world".endsWith("world") === true
5. includes(searchString, [position]): Returns a boolean indicating whether the string includes the specified searchString.
Example: "hello world".includes("world") === true
6. indexOf(searchString, [position]): Returns the index of the first occurrence of the specified searchString, or -1 if not found.
Example: "hello world".indexOf("world") === 6
7. lastIndexOf(searchString, [position]): Returns the index of the last occurrence of the specified searchString, or -1 if not found.
Example: "hello world".lastIndexOf("world") === 6
8. localeCompare(compareString): Compares two strings according to the locale-specific sorting rules.
Example: "hello".localeCompare("hello") === 0
9. match(regexp): Returns an array of matches for the specified regular expression.
Example: "hello world".match(/hello/) === ["hello"]
10. normalize([form]): Returns the Unicode normalization form of the string.
Example: "hello".normalize() === "hello"
11. padEnd(targetLength, [padString]): Pads the string with the specified padString until it reaches the targetLength.
Example: "hello".padEnd(10, "-") === "hello-----"
12. padStart(targetLength, [padString]): Pads the string with the specified padString until it reaches the targetLength.
Example: "hello".padStart(10, "-") === "-----hello"
13. repeat(count): Returns a new string with the specified count of repetitions.
Example: "hello".repeat(3) === "hellohellohello"
14. replace(searchValue, replaceValue): Replaces some or all matches of the searchValue with the replaceValue.
Example: "hello world".replace("world", "universe") === "hello universe"
15. search(regexp): Returns the index of the first match of the specified regular expression, or -1 if not found.
Example: "hello world".search(/world/) === 6
16. slice(start, [end]): Returns a new string containing a portion of the original string.
Example: "hello world".slice(0, 5) === "hello"
17. split(separator, [limit]): Splits the string into an array of substrings using the specified separator.
Example: "hello world".split(" ") === ["hello", "world"]
18. startsWith(searchString, [position]): Returns a boolean indicating whether the string starts with the specified searchString.
Example: "hello world".startsWith("hello") === true
19. substr(start, [length]): Returns a new string containing a portion of the original string.
Example: "hello world".substr(0, 5) === "hello"
20. substring(start, [end]): Returns a new string containing a portion of the original string.
Example: "hello world".substring(0, 5) === "hello"
21. toLocaleLowerCase(): Returns the string converted to lowercase according to the locale-specific rules.
Example: "HELLO".toLocaleLowerCase() === "hello"
22. toLocaleUpperCase(): Returns the string converted to uppercase according to the locale-specific rules.
Example: "hello".toLocaleUpperCase() === "HELLO"
23. toLowerCase(): Returns the string converted to lowercase.
Example: "HELLO".toLowerCase() === "hello"
24. toUpperCase(): Returns the string converted to uppercase.
Example: "hello".toUpperCase() === "HELLO"
25. trim(): Returns the string with whitespace removed from the beginning and end.
Example: " hello ".trim() === "hello"
26. trimEnd(): Returns the string with whitespace removed from the end.
Example: " hello ".trimEnd() === " hello"
Related Posts
See AllQuestion: Discuss the various methods of finding the initial basic feasible solution of a transportation problem and state the...
Introduction Statistics is a crucial branch of mathematics that provides tools for making sense of data. In research, statistics can be...
Introduction In the field of research and data analysis, multivariate techniques are statistical methods used to analyze data that...
Comments