当前位置:网站首页>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));
边栏推荐
- 105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
- node,npm以及yarn下载安装
- npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
- MongoDB基本操作【增、删、改、查】
- [set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
- Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
- Separable bonds and convertible bonds
- 用Three.js做一個簡單的3D場景
- Null and undefined
- Mysql Mac版下载安装教程
猜你喜欢

Limit of one question per day

Makefile demo

TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥

MongoDB安装 & 部署

How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo

The calculation of stripe, kernel and padding in CNN

用Three.js做一个简单的3D场景

ffmpeg录制屏幕和截屏

SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍

Download and install node, NPM and yarn
随机推荐
递归使用和多维数组对象变一维数组对象
float与0比较
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
Ffmpeg recording screen and screenshot
Using jasmine to monitor constructors - spying on a constructor using Jasmine
QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
Don't use the new Dede collection without the updated Dede plug-in
Recursion: quick sort, merge sort and heap sort
Ansible简介【暂未完成(半成品)】
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
2020-01-01t00:00:00.000000z date format conversion
可分离债券与可转债
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
[set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
Yolov5 project based on QT
Makefile demo
Mysql Mac版下载安装教程
Open Visual Studio 2010 hangs when opening a SQL file sql file
The calculation of stripe, kernel and padding in CNN