The first parameter start is the starting index and the second parameter is the length of the substring. Since you’re string can’t have a negative length, you can’t use a negative number for the second parameter (you can use a negative number for the first parameter, though). Of course, that second parameter is optional, if you want the rest of the string.
<font face="arial" size="+1">
Example of Substr()
var straddr = "email@example.com";
document.write("<br>His name is<em> " + straddr.substr(0,14) + "</em>.<br>");
var namesarr = straddr.split("@" );
document.write( "The user name is<em> " + namesarr + "</em>.<br>");
document.write( "and the mail server is<em> " + namesarr + "</em>.<br>");
document.write( "The first character in the string is <em>" + straddr.charAt(0)+ "</em>.<br>");
document.write( "and the last character in the string is <em>" + straddr.charAt(straddr.length - 1) + "</em>.<br>");
1. A string is assigned an e-mail address.
3. The split() method creates an array, called namesarr, by splitting up a string into substrings based on some delimiter that marks where the string is split. This string is split using the @ sign as its delimiter.
4. The first element of the array, namesarr, that is created by the split() method is ecomputernotes, the user name portion of the e-mail address.
5. The second element of the array, namesarr, that is created by the split() method is gmail.com, the mail server and domain portion of the e-mail address.
6. The charAt() method returns the character found at a specified position within a string; in this example, position 0. Position 0 is the first character in the string, a letter e.
7. By giving the charAt() method the length of the string minus 1, the last character in the string is extracted, a letter m.