开发者

Iterate string lines in NodeJS

I get a buffer (and I can make it a string) from child_process.exec()开发者_JAVA技巧 in NodeJS. I need to iterate over the lines of the output string. How would I do this?


One way to avoid splitting the whole thing in memory is to process it one line at a time

var i = 0;
while (i < output.length)
{
    var j = output.indexOf("\\n", i);
    if (j == -1) j = output.length;
    .... process output.substr(i, j-i) ....
    i = j+1;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜