/**
* 等分截取字符串,输入:splitWords('12345',2) 输出 ['12','34',5]
* @param {string} str
* @param {number} num
*/
export function splitWords(str, num) {
if (
Object.prototype.toString.call(str) !== '[object String]' &&
Object.prototype.toString.call(num) !== '[object Number]'
) {
return;
}
if (str.length <= num) {
return [str];
}
let arr = [],
len = str.length,
start = 0;
for (let i = 1; i < len; i++) {
if (i * num > len) {
break;
}
arr.push(str.slice(start, num * i));
start = num * i;
}
arr.push(str.substr(start, len));
return arr;
}
等分截取字符串
最新推荐文章于 2021-03-25 13:47:05 发布