最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript to split string by end of line character and read each line - Stack Overflow

programmeradmin4浏览0评论

I have a need to loop through a large string with several eol characters and read each of these lines looking for characters. I could've done the following but I feel that its not very efficient as there could be more than 5000 characters in this large string.

var str = largeString.split("\n");

and then loop through str as an array

I cant really use jquery and can only use simple JavaScript.

Is there any other efficient way of doing this?

I have a need to loop through a large string with several eol characters and read each of these lines looking for characters. I could've done the following but I feel that its not very efficient as there could be more than 5000 characters in this large string.

var str = largeString.split("\n");

and then loop through str as an array

I cant really use jquery and can only use simple JavaScript.

Is there any other efficient way of doing this?

Share Improve this question edited Nov 15, 2021 at 14:46 Nimantha 6,4716 gold badges30 silver badges76 bronze badges asked Nov 4, 2013 at 3:12 day0opsday0ops 7,48218 gold badges76 silver badges109 bronze badges 4
  • hmmm...you can use web workers, you can parse string in the background. w3schools.com/html/html5_webworkers.asp – StaleMartyr Commented Nov 4, 2013 at 3:18
  • what about using a regular expression? – l2mt Commented Nov 4, 2013 at 3:21
  • 5 This should work just fine - there is no obvious way to do it simpler. 5000 characters is nothing for a modern computer, even a smartphone. Do you have some evidence that this is too slow? – jfriend00 Commented Nov 4, 2013 at 3:24
  • 5000 is just a minimum number I'm expecting. but it could well be 100000 characters. i realize theres no other efficient way of achieving this. for e.g. in java theres StringReader for this purpose. – day0ops Commented Nov 4, 2013 at 3:46
Add a comment  | 

6 Answers 6

Reset to default 6

You could always use indexOf and substring to take each line of the string.

var input = 'Your large string with multiple new lines...';
var char = '\n';
var i = j = 0;

while ((j = input.indexOf(char, i)) !== -1) {
  console.log(input.substring(i, j));
  i = j + 1;
}

console.log(input.substring(i));

Edit I didn't see this question was so old before answering. #fail

Edit 2 Fixed code to output final line of text after last newline character - thanks @Blaskovicz

5000 doesn't seem that intense for a modern JavaScript engine. Of course it depends on what you do on each iteration too. For clarity I recommend using eol.split and [].forEach.

eol is an npm package. In Node.js and CommonJS you can npm install eol and require it. In ES6 bundlers you can import. Otherwise loaded via <script> eol is global

// Require if using Node.js or CommonJS
const eol = require("eol")

// Split text into lines and iterate over each line like this
let lines = eol.split(text)
lines.forEach(function(line) {
  // ...
})

If you are using NodeJS, and have a large string to process line-by-line:

const Readable = require('stream').Readable
const readline = require('readline')

promiseToProcess(aLongStringWithNewlines) {
    //Create a stream from the input string
    let aStream = new Readable();
    aStream.push(aLongStringWithNewlines);
    aStream.push(null);  //This tells the reader of the stream, you have reached the end

    //Now read from the stream, line by line
    let readlineStream = readline.createInterface({
      input: aStream,
      crlfDelay: Infinity
    });

    readlineStream.on('line', (input) => {
      //Each line will be called-back here, do what you want with it...
      //Like parse it, grep it, store it in a DB, etc
    });

    let promise = new Promise((resolve, reject) => {
      readlineStream.on('close', () => {
        //When all lines of the string/stream are processed, this will be called
        resolve("All lines processed");
      });
    });

    //Give the caller a chance to process the results when they are ready
    return promise;
  }

You could read it character by character manually and call a handler when you get a newline. It is unlikely to be more efficient in terms of CPU usage but will likely take up less memory. However, as long as the string is less than a few MBs, it should not matter.

function findChar(str, char) {
    for (let i = 0; i < str.length; i++) {
        if (str.charAt(i) == char) {
            return i
        }
    }
    return -1
}

So, you know how to do it, you're just making sure there's no better way to do it? Well, I'd have to say the way you've mentioned is exactly it. Although you may want to look up a regex match if you're looking for certain text split by certain characters. A JS Regex Reference can be found Here

This would be useful if you know how the text is going to be setup, something akin to

var large_str = "[important text here] somethign something something something [more important text]"
var matches = large_str.match(\[([a-zA-Z\s]+)\])
for(var i = 0;i<matches.length;i++){
   var match = matches[i];
   //Do something with the text
}

Otherwise, yes, the large_str.split('\n') method with a loop is probably best.

发布评论

评论列表(0)

  1. 暂无评论