In this tutorial, we will learn about JavaScript substring and how we can get a substring from a string using javascript methods.
A substring is a sequence of characters within a string, in simple words you can say that substring is a part of the main string and could be any character of the string.
For example, “Your Blog Coach” is the main string, and “Blog” is a substring of the main string “Your Blog Coach”. It could be of any length from 1 to the whole main string.
See Also: Find the Longest Word in a String Using JavaScript
So let’s get started.
Get Substring From String in JavaScript
To extract the substring from a string in javascript, we will use some of javascript’s built-in methods. You can use any method that fits easily to you.
- substr()
- substring()
- slice()
Let’s see in detail these javascript methods to get substring with the help of different examples.
Extract Substring Using substr() Method
The substr()
method is used to extract the part of a string. It will start from a specified position and return the specified length of characters.
If you want to get the substring from the end of the string then you have to specify a negative (-) start position.
Let’s use the substr()
method to get the substring from the string and see examples of it.
Get Substring From Start
To get the substring from the beginning of the string, you need to specify the 0 start index as the first parameter in the method.
You can also pass the second parameter as the length of characters that how many you want to extract, it is an optional parameter.
Output
Your
Note: The length parameter also counts the spaces, so give the length parameter value accordingly.
Get Substring From End
To get the substring from the end of the string, you have to specify the negative (-) start index. So it will start from the end and return the specified length of characters.
Output
Coach
Extract Substring Using substring() Method
Now we will use the substring()
javascript method to extract the substring from a string. The substring()
method is used to get the substring between the start position and to end position.
In this method, you cannot give the negative index to the start position, it will treat the negative number as a zero.
Let’s see some examples of the substring() method.
Example with Start and End Index
Output
Your
Example without End Index
Output
Blog Coach
Extract Substring Using slice() Method
You can also use the slice()
javascript method to get the substring from a string. It also accepts the two parameters, start and end index.
You have to specify the start index and end index to extract the part from the string and return you a new substring.
Example with Start and End Index
Output
Blog
Example without End Index
Output
g Coach
Example Only Last Character
Output
h
So these are the methods of javascript that you can use to get the substring from a string in many different ways.
Hope this tutorial is helpful for you, and if you like it please share it with your friends or in groups. If you have queries please ask me in the comment section, I will respond to you as soon as possible.