lastindexof method
returns the index within the calling string object of the last occurrence of the specified value. the calling string is searched backwards, starting at fromindex.
语法
stringname.lastindexof(searchvalue, [fromindex])
stringname is any string or a property of an existing object.
searchvalue is a string or a property of an existing object, representing the value to search for.
fromindex is the location within the calling string to start the search from. it can be any integer from 0 to stringname.length - 1 or a property of an existing object.
method of
描述
characters in a string are indexed from left to right. the index of the first character is 0, and the index of the last character is stringname.length - 1.
if you do not specify a value for fromindex, javascript assumes stringname.length - 1 (the end of the string) by default. if searchvalue is not found, javascript returns -1.
例子
the following example uses indexof and lastindexof to locate values in the string "brave new world".var anystring="brave new world"
//displays 8
document.write("<p>the index of the first w from the beginning is " +
anystring.indexof("w"))
//displays 10
document.write("<p>the index of the first w from the end is " +
anystring.lastindexof("w"))
//displays 6
document.write("<p>the index of 'new' from the beginning is " +
anystring.indexof("new"))
//displays 6
document.write("<p>the index of 'new' from the end is " +
anystring.lastindexof("new"))
相关
lastmodified property
a string representing the date that a document was last modified.
语法
document.lastmodified
property of
描述
lastmodified is a read-only property.
例子
in the following example, the lastmodified property is used in a <script> tag at the end of an htm file to display the modification date of the page:
document.write("this page updated on " + document.lastmodified)
length property
an integer that specifies a length-related feature of the calling object or array.
语法
when used with objects:
1. formname.length 2. framereference.length 3. history.length 4. radioname.length 5. selectname.length 6. stringname.length 7. windowreference.length
when used with array properties:
8. anchors.length 9. elements.length 10. forms.length 11. framereference.frames.length 12. windowreference.frames.length 13. links.length 14. selectname.options.length
formname is either the name of a form or an element in the forms array.
framereference is either the value of the name attribute of a frame or an element in the frames array.
radioname is either the value of the name attribute of a radio object or an element in the elements array.
selectname is either the value of the name attribute of a select object or an element in the elements array.
stringname is any string or a property of an existing object.