Web Development Certification Training
- 4k Enrolled Learners
- Live Class
A substring is a subset of another string. It is a contiguous sequence of characters within a string.
For example, “Welcome to Edureka” is a substring of the string “Welcome to Edureka Online Courses“. But, “Edureka Courses” is not a substring as it is a subsequence or the generalization of the string.
This substring method returns the new sub-string based on given parameters.
Let’s take an example and see how the substring() function works.
The length property is used to get the length of a string. The substring() method and length property are used together to extract the last characters of a particular string. This method is easier to remember as you don’t need to know the starting and ending indices.
var anyString = 'Edureka'; var anyString1 = anyString.substring(anyString.length - 5); //Displays last 5 characters console.log(anyString1);
These methods are used to extract parts of a string and return the extracted parts in a new string. All of these do not change the original string.