当前位置:网站首页>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));
边栏推荐
- Separable bonds and convertible bonds
- 【DRM】DRM bridge驱动调用流程简单分析
- Leetcode: dynamic planning template
- Shardingsphere dynamic data source
- TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
- Bigvision code
- umi 路由拦截(简单粗暴)
- Stepping on pits and solutions when using inputfilter to limit EditText
- Mongodb master profile
- 错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
猜你喜欢

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

ffmpeg录制屏幕和截屏

For instruction, uploading pictures and display effect optimization of simple wechat applet development

IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥

Error in compiled file: error: unmapped character encoding GBK

机械臂速成小指南(八):运动学建模(标准DH法)

递归:一维链表和数组

Makefile demo

Summary of electromagnetic spectrum

NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
随机推荐
Captura下载安装及在Captura配置FFmpeg
[embedded module] OLED display module
Table structure of Navicat export database
[MySQL] the difference between left join, right join and join
机械臂速成小指南(八):运动学建模(标准DH法)
shardingsphere动态数据源
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
Mysql Mac版下载安装教程
Limit of one question per day
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Web会话管理安全问题
Applet get user avatar and nickname
Makefile demo
Pytoch lightweight visualization tool wandb (local)
float与0比较
Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
Download and install captura and configure ffmpeg in captura
Introduction à mongodb
Message queue addition failure