javascript - 如何根据某些规则制作一个中间有空格的字符串
问题描述
我正在编码一组字符串,在输出时,我想得到一个由空格分隔的字符串,像这样
“imtgdvs 恐惧者 mayoogo anouuio ntnnlvt wttddes aohghn sseoau”
代替
“imtgdvs”
“恐惧者”
“梅优果”
“阿努伊奥”
“ntnnlvt”
“wtddes”
“啊啊啊”
“sseo”
normalisedText = "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots";
const cols = Math.ceil(Math.sqrt(textCount));
const rows = Math.ceil(textCount / cols);
const textArray = [];
let encodedChunks = "";
let cypherText = "";
let startIndex = 0;
for (let i = 0; i < cols; i ++) {
for (let j = i; j < normalisedText.length; j += cols) {
cypherText += normalisedText[j];
}
cypherText += '\n';
}
console.log(cypherText);
解决方案
尝试这个:
textCount = 5
normalisedText = "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots";
const cols = Math.ceil(Math.sqrt(textCount));
const rows = Math.ceil(textCount / cols);
const textArray = [];
let encodedChunks = "";
let cypherText = "";
let startIndex = 0;
for (let i = 0; i < cols; i ++) {
for (let j = i; j < normalisedText.length; j += cols) {
cypherText += normalisedText[j];
}
cypherText += ' ';
}
console.log(cypherText);
推荐阅读
- google-bigquery - BigQuery - 从非分区表转换为分区表 - 旧版 SQL
- java - Java 从 XML 网站获取数据到 SQL 数据库
- vue.js - 使用 Vue CLI 热重载 scss?
- reactjs - 带有 React 的 SignalR 不是很实时
- c++ - 多字符字符常量变量的值是多少
- android - 如何调整按钮中图标的大小和颜色?
- xamarin - 后台服务位置已更改未触发 Xamarin Android
- node.js - 在 Docker compose 中运行 gulp - 不创建文件
- vb.net - 从 HEX 中的驱动程序获得答案
- python - 使用 iterrows 对最后一次迭代值执行操作