簡體   English   中英

將JavaScript(nodeJS)字符串拆分為固定大小的塊

[英]Split JavaScript(nodeJS) string into fixed sized chunks

我想使用空格作為定界符在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));

它適用於普通文本,但是如果文本包含換行符,則會出現以下錯誤:
語法錯誤:意外的令牌非法
在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._load(module.js:310:12)
在Function.Module.runMain(module.js:501:10)
在啟動時(node.js:129:16)
在node.js:814:3

 // 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); 

如果要刪除尾隨空格,可以使用trim()

slices.push(str.slice(0, lastSpace).trim());

我本來試圖實現Randy的答案,但是發現我需要不同的實現:

  • 批量大小作為參數,
  • 不喜歡while循環
  • 並認為無需拆分字符串就可以完成。

這是我的解決方案:

 var testString = "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 stringChunks(str, chunkSize) { chunkSize = (typeof chunkSize === "undefined") ? 140 : chunkSize; let resultString = ""; if ( str.length>0 ) { let resultArray = []; let chunk = ""; for ( let i = 0; i<str.length; i=(i+chunkSize) ) { chunk = str.substring(i,i+chunkSize); if ( chunk.trim()!="" ) { resultArray.push(chunk); } } if (resultArray.length) { resultString = resultArray.join("/\\n"); } } else { resultString = str; } return resultString; } console.log( stringChunks(testString) ); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM