一、字符串截取:
1.slice(start,[end])
2.substring(start,[end])
3.substr(start,[length])
4.通过特定字符截取:
function getCaption(obj) {//通过字符截取
var index = obj.lastIndexOf("\_");
obj = obj.substring(index + 1, index + 2);
return obj;
}
5.利用正则表达式获取
var num = s.replace(/[^0-9]/ig, "");//正则获取字符串中的数字
注:index都是从0开始
二、数组截取:
1.splice() 方法
arrayObject.splice(index,howmany,item1,…,itemX)
参数 描述
index 必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。
howmany 必需。要删除的项目数量。如果设置为 0,则不会删除项目。
item1, …, itemX 可选。向数组添加的新项目。
参考:http://www.w3school.com.cn/jsref/jsref_splice.asp
2.使用示例,用splice方法来清空数组
s.splice(0, s.length);
for (var i = 0; i < response.data.rows.length; i++) {
if (response.data.rows[i].Number.length==11) {
s.push(response.data.rows[i].Number.substring(8, 11));
}
else {
s.push(response.data.rows[i].Number);
}
}