|
You use one of the methods substring, substr or slice of the String type.
- slice takes two arguments, the starting position and the ending position.
- substr takes two arguments, the starting position and the length of the substring.
If you use a negative position, it will count from the end of the string
- substring behaves the same as slice, except that it doesn't allow its arguments to be negative and thus point from the end of the string.
All of these will terminate at the end of the string if the second argument is left out.
(You can also use the property length of the string to find out how long the string is and work based on that. Remember that JavaScript is zero based, in other words a string of the length 3 will have characters at the positions 0, 1 and 2.)
"string".slice(2,4) => "ri"
"string".substr(1,3) => "tri"
"string".substr(-1) => "g"
"string".slice(-5, -2) => "tri"
Last edited by liorean; 05-24-2003 at 02:08 PM..
|