当前位置: 首页 > 知识库问答 >
问题:

将JavaScript(nodeJS)字符串拆分为固定大小的块

韩彦君
2023-03-14

我想在JavaScript中使用空格作为分隔符将字符串分成固定大小的块(例如每个140个字符)(即它不应该分词),注意:它应该处理换行符目前我使用的是wordwrap npm包,但它不处理新行符。

var wrap = require('wordwrap')(140)    
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
console.log(wrap(str));

它适用于普通文本,但如果文本包含新行标记,我会得到以下错误:
syntaxerror:exports.runinthiscontext(VM.js:73:16)
在module._compile(module.js:443:25)
在object.module._extensions.js(module.js:478:10)
在module.load(module.js:355:32)
在function.module.js:310:12)
在约814:3

共有1个答案

蓝苗宣
2023-03-14
// define string variable
var string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"

function sliceMyString(str){
// initialize array (not required but verbose)
    var slices = [];

// while string is not empty
// take 140 characters
// check witch one was the last space or if the end of the line is reached
// then => push them in slices
// then => remove them from the string

    while(str != ''){
        var lastSpace = 0;

        for(var i = 0; i < str.length && i < 140; i ++){
            if(str[i] == ' '){
                lastSpace = i;
            }
            if(i == str.length - 1){
                lastSpace = str.length;
            }
        }

// insert into array (including trailing space, see below the codeblock)
        slices.push(str.slice(0, lastSpace));
        str = str.slice(lastSpace);
    }

// just logging the variables in the slices array

    slices.map(function(slice){
        console.log(slice);
    });
}

sliceMyString(string);
 类似资料:
  • 问题内容: 我想将一个非常大的字符串(例如10,000个字符)分割成N个大小的块。 就性能而言,最佳方法是什么? 例如: 被2分割成。 使用这种方法是否可能实现?如果可以,从性能角度来看,这是否是最佳方法? 问题答案: 您可以执行以下操作: 该方法仍可用于大小不是块大小的整数倍的字符串: 通常,对于要从中提取最多 n个 大小的子字符串的任何字符串,都可以执行以下操作: 如果您的字符串可以包含换行符

  • 问题内容: 我将如何能够采取像一个字符串 ,并将其分成4个长度元组像(,,) 问题答案: 用途:

  • 本文向大家介绍JavaScript 将字符串拆分为数组,包括了JavaScript 将字符串拆分为数组的使用技巧和注意事项,需要的朋友参考一下 例子 用于.split从字符串到拆分子字符串的数组: 使用数组方法 .join返回字符串:            

  • 我在一个名为string1到String7的程序中有七个字符串。 每个字符串的大小为30个字符。

  • 问题内容: 我正在尝试找到一种将String拆分为String数组的方法,并且每当遇到白色香料时就需要对其进行拆分,例如 “嗨,我是保罗” 进入” “嗨”“我”“保罗” 如何使用RegularExpression在split()方法中表示空格? 问题答案: 您需要一个正则表达式,例如,这意味着: 每当遇到至少一个空格时就进行拆分 。完整的Java代码是:

  • 问题内容: 我想按给定大小的块分割字符串 范例: 字符串和输出应为 问题答案: 您可以每隔n个元素对集合元素(在本例中为Characters)进行分组,如下所示: 编辑/更新 : 如果要将超出的字符追加到最后一组: