如何将字符串按长度分割成多个子串?
- 内容介绍
- 文章标签
- 相关推荐
本文共计178个文字,预计阅读时间需要1分钟。
以下是对给定代码的简化版本:
javascriptfunction splitByLength(src, len) { if (src==null) { throw new Error(src 不能为 null 或 undefined); } if (src===) { return []; } if (Object.prototype.toString.call(src) !==[object String]) { throw new Error(src 必须是字符串); } let result=[]; for (let i=0; i function splitByLength(src, len) {
if (src === null || src === undefined) {
throw new Error("src 不能为 null 或 undefined");
}
if (src === "") {
return [];
}
if (Object.prototype.toString.call(src) !== "[object String]") {
throw new Error("src 必须是 String 类型");
}
len = parseInt(len);
if (!len || len <= 0) {
throw new Error("分割的长度不合法");
}
if (src.length <= len) {
return [src];
}
let result = [];
let sIndex = 0;
let eIndex = len;
while (src.length > sIndex) {
result.push(src.slice(sIndex, eIndex));
sIndex += len;
eIndex += len;
}
return result;
}
本文共计178个文字,预计阅读时间需要1分钟。
以下是对给定代码的简化版本:
javascriptfunction splitByLength(src, len) { if (src==null) { throw new Error(src 不能为 null 或 undefined); } if (src===) { return []; } if (Object.prototype.toString.call(src) !==[object String]) { throw new Error(src 必须是字符串); } let result=[]; for (let i=0; i function splitByLength(src, len) {
if (src === null || src === undefined) {
throw new Error("src 不能为 null 或 undefined");
}
if (src === "") {
return [];
}
if (Object.prototype.toString.call(src) !== "[object String]") {
throw new Error("src 必须是 String 类型");
}
len = parseInt(len);
if (!len || len <= 0) {
throw new Error("分割的长度不合法");
}
if (src.length <= len) {
return [src];
}
let result = [];
let sIndex = 0;
let eIndex = len;
while (src.length > sIndex) {
result.push(src.slice(sIndex, eIndex));
sIndex += len;
eIndex += len;
}
return result;
}

