- String_Object: Please specify the valid string from which you want to extract.
- Start: Please specify the index position from where you want to extract.
- Length: This argument is optional. Please specify the number of characters you want to extract.
- If you omit the second index then, SubStr() function will start from the first index position and continue to the last
- If you are using the Negative numbers as first index then, SubStr() function starts from right to left.
- If you are using the Negative numbers or Zero as Length argument then, SubStr() function will return Empty string.
First we declared the String variable Str1 and assigned corresponding value using following statement
var Str1 = "Tutorial Gateway";
Following statement will extract five characters from the original string, starting at index position 2
var Str2 = Str1.substr(2, 8);
Next, We omitted the second argument which means SubStr() function will start at index position 2 and ends when it reaches last.
var Str4 = Str1.substr(2);
Within the following statements, we used Negative and Zero values as second argument. As we said before, this will return empty string
var Str5 = Str1.substr(2, -3); var Str6 = Str1.substr(2, 0);
Next, we used the negative indexing. Following statement will start extracting from -3 (index position of 3 from right to left) and ends at last position.
var Str7 = Str1.substr(-3);
Thank You for Visiting Our Blog