当前位置:网站首页>Intercept string fixed length to array
Intercept string fixed length to array
2022-07-03 03:43:00 【youhebuke225】
The first method ( recommend )
function sliceFixLenth(str,length) {
const result = [];
for (let i = 0; i <str.length ; i+=length) {
result.push(str.slice(i,i+length));
}
return result;
}
let str = "helloworld"
console.log(sliceFixLenth(str,1)); // ['h', 'e', 'l', 'l','o', 'w', 'o', 'r','l', 'd']
console.log(sliceFixLenth(str,2));// [ 'he', 'll', 'ow', 'or', 'ld' ]
console.log(sliceFixLenth(str,3));// [ 'hel', 'low', 'orl', 'd' ]
The second method
As an extension
function sliceFix(str,length) {
const count = Math.ceil(str.length / length);
let start = 0;
let end = 0;
const result = [];
for (let index = 0; index < count; index++) {
end += length;
result.push(str.slice(start,end))
start += length;
}
return result;
}
console.log("sliceFix",sliceFixLenth(str,3));
边栏推荐
- Message queue addition failure
- 递归:深度优先搜索
- 没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
- Compare float with 0
- Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
- 动态规划:最长公共子串和最长公共子序列
- MongoDB安装 & 部署
- C language hashtable/hashset library summary
- The series of hyperbolic function in daily problem
- TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
猜你喜欢
Limit of one question per day
MongoDB安装 & 部署
Pytorch multi card distributed training distributeddataparallel usage
Recursion: quick sort, merge sort and heap sort
Pytorch轻量级可视化工具wandb(local)
FileZilla client download and installation
Lvgl usage experience
Recursion: depth first search
Captura下载安装及在Captura配置FFmpeg
Table structure of Navicat export database
随机推荐
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
Ffmpeg recording screen and screenshot
redis高级应用【密码防护、数据持久化、主从同步、哨兵模式、事务】【暂未完成(半成品)】
MongoDB主配置文件
渤、黄海的潮汐特征
TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
umi 路由拦截(简单粗暴)
Role of JS No
[mathematical logic] normal form (conjunctive normal form | disjunctive normal form | major item | minor item | maximal item | minor item | principal conjunctive normal form | principal disjunctive no
递归:深度优先搜索
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
[mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
阿洛对自己的思考
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
Mysql Mac版下载安装教程
Mongodb installation & Deployment
Use of sigaction
8.8.2-PointersOnC-20220214