一、截取
1.slice
let str ="122889,";
str=str.slice(0,str.length-1);
console.log(str);
let str ="122889";
str=str.slice(0,str.length-1);
console.log(str);
2.substr
let str ="122889,";
str=str.substr(0,str.length-1);
console.log(str);
3.substring
let str ="122889,";
str=str.substring(0,str.length-1);
console.log(str);
4.JS截取字符串中的数字
4.1.使用正则
var str = '价格4500元';
var num = str.replace(/[^0-9]/ig,'');
alert(num);
二、字符串分割成数组
1.split()
var str="How are you doing today?";
var n=str.split(" ");
console.log(n);
2.es6里提供的扩展运算符(…)
let str = 'abc' [...str]
三、数组转换字符串
1.join()
var arr = new Array(3);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr.join());
var arr = new Array(3);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
document.write(arr.join("."));
四、字符串插入
1.insertStr
var month="201910";
var newmonth=insertStr(month,4,".");
function insertStr(soure, start, newStr){
return soure.slice(0, start) + newStr + soure.slice(start);
}