What is a JS Substring?
A sub-string is a string that is part of another string.
We will learn how to obtain a sub-string using the three built-in methods distinguished in this article.
The sub-method ().
Let’s first look at the substring () technique.
- This technique extracts some of the original string and replaces it with a new string
- Two parameters are required in the sub-method
- The substring () function pulls characters from a string and returns the string between two metrics (positions)
- The substring () method deletes characters from the beginning to the end of a string (exclusive)
- The original string is not modified by the substring () technique.
- If the beginning is larger than the end, the two arguments will be replaced, (4, 9) = (9, 4)
- Start and end are considered 0 if they are less than 0
Some more considerations:
The sub-string method causes an empty string if startIndex = endIndex. It returns an empty string if startIndex and endIndex are larger than the length of the string.
let text = "Hello Codeunderscored!"; console.log(text.substring(24, 30))
Now, let’s define the parameters in the syntax above.
- It indicates the starting point where to start extraction. Typically, index 0 is the index of the first character.
- It is optional to have the end parameter
- This location indicates the stopping point of the rescue. However, it usually reaches this value but does not include. If the chance is dropped, then that’s the rest of the string.
As shown in the example below, any non-numeric value (NaN) is treated as zero.
const str = "Codeunderscored is simply scaling the heights!"; console.log(str.substring('start', 3));
The ‘Start’ argument in the example above is not a number. As a result, the generated string starts at a zero position of str. In addition, the substring () function returns a copy of the string it is running on if no arguments are provided and both parameters are omitted.
Example 1: Extracting a string starting from the third position
let text = "Hello Codeunderscored!"; console.log(text.substring(3));
Example 2: A sub-string when the start point is larger than the end
If startIndex is more than endIndex, the sub-string method replaces the arguments and returns a sub-string, assuming the endIndex is larger than startIndex.
let text = "Hello Codeunderscored!"; console.log(text.substring(30, 4))
Example 3: In case the starting point is negative (less than 0), reset the starting index to 0:
let text = "Hello Codeunderscored!"; console.log( text.substring(-2));
Example 4: Extract the first value From a string
let text = "Hello Codeunderscored!"; console.log( text.substring(0, 1));
Example 5: How to extract the last value In the given string
let str = "Hello Codeunderscored!"; console.log(str.substring(str.length – 1));
Substrate method ()
According to Mozilla’s documentation, the substr () method is a previous generation function that should not be used. However, we will explain what this does because it may appear in some previous projects, or you may find it useful.
The substr () method returns a sub-string of the original string and requires the following two parameters:
- startIndex: Specifies the starting point of the sub-length
- Length: The total amount of characters to include (optional)
The difference with substring () is that the subst () method expects length instead of endIndex as the second parameter:
This example counts four characters beginning with the provided startIndex (2) and returns them as a sub-string.
If the second parameter is not set, it returns to the end of the original string (similar to the previous string method). The following is an example.
Slice method ()
Similar to the substring () method, the slice () method returns a sub-string of the original string. Moreover, the same two parameters are required in the slice method:
- startIndex: Indicates the beginning of the sub-string.
- endIndex: This is the endpoint of the string and is optional most of the time.
Similarities between the substring () and slice () methods.
Here are some of the similarities between the substring () and slice () methods.
We will get a sub-string from the index number specified to the end of the original text if we do not define an end index as shown below.
If we set both startIndex and endIndex, we get the characters in the original text between the specified index numbers:
An empty string is returned if startIndex and endIndex are larger than the length of the string.
Differences between slice () and other methods:
An empty string is returned by the slice () method if startIndex> endIndex.
If startIndex is a negative value, the first character in the string is called from the end (inverse):
We hope you found this article helpful. If so, share your thoughts with us.