如何通过按长度分割来处理字符串.txt文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计180个文字,预计阅读时间需要1分钟。
按长度分割字符串的函数如下:
javascriptfunction 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 必须是字符串); } 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; }
本文共计180个文字,预计阅读时间需要1分钟。
按长度分割字符串的函数如下:
javascriptfunction 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 必须是字符串); } 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; }

