180
Chapter 3
using substring in javascript
1Jim
0
1
2
3
Position:
myString.substring(1, 3);
myString
myString.substring(2, myString.length);
myString.substring(0, myString.length);
Ji
im
1Jim
You can use myString.length to get
the length of a string, which will
always be one greater than the
last position in the string.
4
myString.length
This substring will start at position
1, end at position 3, and not
include the character at position 3.
substring() practice
Let’s spend a little time practicing how to use the JavaScript substring()
method, using the string you saw on the last two pages.
JavaScript
PHP script
HTML form
This substring started at position 1
(“J”), and went to position (3 - 1),
which is position 2 (“i”).
Since myString.length will be 1 greater
than the last position, this will always
go to the end of a string.
Position 0 is the rst
character in the string...
...and myString.length will always
return to the end of the string.
This JavaScript would always
return the entire string.