What is String in JavaScript?
JavaScript strings are sequences of characters, including letters, numbers, and symbols. Unicode characters can be encoded in JavaScript strings using the "\uXXXX" syntax. In JavaScript, strings are primitive and immutable data types, which means they cannot be changed after they have been created. JavaScript provides many sets of built-in functions for concatenating, containing, splitting, reversing, and trimming strings. You can also convert string to array, check if it starts or ends with another string, and get the length of a string.
What is substring?
A substring is a part of a string, which can be a single character, the entire string, or any other piece of the string. Substrings are not limited to words and can contain characters from the middle of a word. A substring is also a string, and the same methods can be used for a substring as for a string. JavaScript has this built-in string.substring() function which returns the substring of a given string.
Where:
- start: a number specifies the starting position from which to copy the substring from the source string. In JavaScript, the indexing starts from zero.
- end (optional): a number indicating the end position up to which the substring is copied
JavaScript Substring Examples
Below are some examples of extracting substrings from strings in JavaScript:
Extracting a substring from the beginning of a string
The following is an example of extracting a substring starting from the first position of the string:
Extracting the last characters of a string
The following is an example of extracting a substring from the specified index up to the end of the string:
Extracting a substring from the middle of a string
The following is an example of extracting a substring from the middle of a string:
How to search for a substring in a string in JavaScript?
To find a substring in a string in JavaScript, you can use the indexOf() method. The indexOf() method returns the first occurrence of the specified substring in the string, or "-1". The method is case-sensitive.
What is the difference between substr() and substring() in JavaScript?
The difference between string.substr(start, length) and string.substring(start, end) is in the second parameter. The second parameter for the string.substr() method is the number of characters to extract, and for the string.substring() is the index up to which to extract. Also, substr() can take a negative starting position as an offset from the end of the string, but substring() cannot. The substr() function is deprecated and may be removed in future versions of JavaScript.
What is the difference between slice() and substring() in JavaScript?
The string.slice(start, end) and string.substring(start, end) methods are almost identical, but they differ in handling negative arguments.
Method | Common | Differences |
---|---|---|
string.substring(start, end) | 1. If the start is equal to the end an empty string is returned. 2. If the end is omitted, it will extract all characters from the start position to the end of the string. 3. If any of the arguments is greater than the length of the string, the length will be used instead. 4. Do not change the original string, but return a new string. | 1. If start > end, then substring() will swap these 2 arguments. 2. If any of the arguments is negative or equal to NaN, it is treated as if it were negative 0. |
string.slice(start, end) | 1. If start > end, then slice() will return an empty string. |
Extracting a substring from a string using the slice() method
The following is an example of how to extract a substring from a string using the slice() method: