当前位置:网站首页>将字符串按长度截取成数组
将字符串按长度截取成数组
2022-06-21 20:37:00 【我是来写bug的吧】
/**
* @param string text 为传入的文本
* @param int num 为单行显示的字节长度
* @return array
*/
function textByteLength(text, num) {
let strLength = 0;
let rows = 1;
let str = 0;
let arr = [];
for (let j = 0; j < text.length; j++) {
// console.log(text.charCodeAt(j));
if (text.charCodeAt(j) > 255) {
// console.log('if');
strLength += 2;
if (strLength > rows * num) {
// console.log(3333);
// strLength++;
console.log(strLength);
arr.push(text.slice(str, j));
str = j;
rows++;
}
} else {
// console.log('else');
strLength++;
if (strLength > rows * num) {
arr.push(text.slice(str, j));
str = j;
rows++;
}
}
}
arr.push(text.slice(str, text.length));
// return [strLength, arr, rows] // [处理文字的总字节长度,每行显示内容的数组,行数]
console.log(strLength); // 65
console.log(arr); // Array(4) [ "今天又到了星期五,明", "天就是周六了,后天就是", "周日了,大后天就是周", "一哈哈" ]
console.log(rows); // 4
}
textByteLength('今天又到了星期五,明天就是周六了,后天就是周日了,大后天就是周一哈哈',20)边栏推荐
- 利用while循环,分别计算1-100中奇数的和、偶数的和【方法一】
- 如何卸载用conda命令安装的包
- Pal2nal| how to run pal2nal from the command line
- [deeply understand tcapulusdb technology] tmonitor background one click installation
- 安超云入选《CIOReview》2022年亚太区“十大云计算解决方案提供商”
- Technology sharing | mysql:caching_ sha2_ Password quick Q & A
- 力扣刷題集結4(mysql版本)
- 中衍期货开户可以吗?平台靠谱吗?安全性高吗?
- 洛谷P1378 油滴扩展 题解
- 利用while循环,分别计算1-100中奇数的和、偶数的和【方法二】
猜你喜欢

技术分享 | MySQL中一个聚类增量统计 SQL 的需求

浅学Vector---如何使用常见的接口

Enterprise data leakage prevention solution sharing

Self made C compiler

HiCPlotter|HiC数据可视化工具

001 new construction project based on opencvsharp
![[deeply understand tcapulusdb technology] transaction execution of document acceptance](/img/7c/25a88f46e02cebd2e003b9590b9c13.png)
[deeply understand tcapulusdb technology] transaction execution of document acceptance

联系五心红娘脱单
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/a8/04c4c6703a6137d17666f211d7988d.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

MitoZ|Multi-Kmer mode
随机推荐
IQtree|构建进化树的软件
类库、框架、模块、组件等概念介绍
从-1开始实现一个中间件
Font size in DataGridView of C #
Chess and card games
UEFI 双系统+双硬盘安装
UEFI dual system + dual hard disk installation
How to uninstall a package installed with the CONDA command
001 new construction project based on opencvsharp
An example of CPU instruction rearrangement leading to errors
Luogu p1378 oil drop expansion problem solution
HiCPlotter|HiC数据可视化工具
GDB debugging skills (0) getting started with GDB
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Technology sharing | introduction to kubernetes pod
pi4j针脚模拟总线,进行控制传输和数据传输的几种思路
window安装scoop的国内镜像
Is it OK for Zhongyan futures to open an account? Is the platform reliable? Is it safe?
PAML|计算dN/dS值的生信软件
线粒体基因组常见缩写与术语