3.24. Introducing the JavaScript substring()
function
JavaScript has a function that's just perfect for taking a string like the coffee maker is returning, and breaking it up into a few parts. Here's how you use the substring() function:
var newStringnewString is the
smaller substring returned by JavaScript. = myStringmyString
is the variable that holds the string you want to break up into several smaller
parts..substringsubstring()
is the JavaScript function you can use to break down strings into
smaller parts.(startIndex,startIndex is the position
in myString you want your substring to begin at. Remember, "0" is the position of
the first character, not "1"., endIndex);endIndex is the
position in myString that you want your substring to end at. The character at this
poisition is not included in the substring.
It may help you to think of substring() as creating a string from position
startIndex to position (endIndex - 1).
myString
1Jim
Position: 0 1 2 3 myString.length4You can
use myString.length to get the length of a string, which will always be one
greater than the last position in the string.
|