js通过 indexOf() 方法与 substring(vstart,vstop) 方法结合使用来截取字符串:
var str = "abcdeafcs";
012345678
str.indexOf("c"):得到c第一次出现的位置:2 ; 位置(索引)从0开始,所以该方法也可以判断一个字符串中是否存在一个字符串,特殊字符需要进行转义。
str.substring(0,3):得到字符串:“abc”,含头不含尾;位置从0开始。vstart:必须,vstop:不必须,如果不指定则读完字符串。
如果要截取“dea”这个字符串: str.substring(str.indexOf("dea"),str.indexOf("dea") + 3)。
str.substr(0,3):得到字符串:"abc";位置从0开始,截取3个字符。vstart:必须,vlength:不必须,如果不指定则读完字符串。