0

I've been trying to make my p5.js program try to read a text file and notice when it hits a new paragraph, but I'm not exactly sure how to do it. for example, right now I'm using 7 paragraphs of just Lorem Ipsum and each paragraph is divided by an entire empty line. how could I make p5.js notice this empty line and count the paragraphs?

i've already tried detecting if there was at least 2 spaces but i'm not sure if I just did it wrong or it doesn't work.

1 Answers1

0

Assuming you want to find every index of a line break in the text file so you can handle them accordingly.

var str = "This is a line.\nThis is another line.\nThis is a third line.";
var indices = [];
for(var i=0; i<str.length;i++) {
    if (str[i] === "\n") indices.push(i);
}

The indices array holds the indexes of every \n character in the string.

Note: Be careful of different line break characters