实例编写:
<script language="javascript" type="text/javascript">
var str="hello you are";
document.write(str.lastIndexOf("h")+"<br />");
document.write(str.lastIndexOf(" ")+"<br />");
document.write(str.lastIndexOf("l")+"<br />");
document.write(str.lastIndexOf("yo")+"<br />");
document.write(str.lastIndexOf("w")+"<br />");
document.write(str.substring("3")+"<br />");
document.write(str.substring("6")+"<br />");
document.write(str.substring("9")+"<br />");
document.write(str.substring(3,7)+"<br />");
document.write(str.substring(str.lastIndexOf(" ")));
document.write(str.substring(str.lastIndexOf("yo"))+"<br />");
</script>
实例结果:
0
9
3
6
-1
lo you are
you are
are
lo y
are
you are
换行用<br />
从字符串后面开始取位用lastIndexOf("s")
取子串用substring(startnum,tonum) 或 substring(startnum)