string.substring() method extracts a substring between the two specified indices in the parent string.
- start: is the index of the first character from which you want to start the extraction.
- end: is the index of the last character you want to end the extraction.
Suppose we pass a single numeric value into the function. In that case, the program will recognize it as a start parameter, so you can use this method to remove the first two characters of the string by passing it parameter equals “2”. Please pay attention to the code below to understand how it works.
var string = "Hi Welcome to LearnShareIT"; var newString = string.substring(2); console.log(newString);
" Welcome to LearnShareIT"
With only one numeric value passed, the end parameter is automatically defined by the function as the last index of the string, which means we will get a string starting from the second value to the end of the string. This is precisely what we need to do in this article. Follow the next steps to know more ways to do this topic.
Or you can also use the
slice() function with the input string and pass it a parameter of two, which can return a value similar to using a substring. All we need to do is replacing the
substring() function with the
var string = "Hi Welcome to LearnShareIT"; var newString = string.slice(2); console.log(newString);
slice() method, you can also pass it a negative value so that different substrings can be obtained in exceptional cases. If the start is a negative number,
slice() will position the count from the end of the string, just like
substring(). In the case of counting from the end of the string, but the start position still exceeds the end position, it will be like the case start > stop. Good luck with these two methods.
Maybe you are interested:
Name of the university: PTIT