0

I'm just starting with node.js and i don't know how to figure out this thing. I have a .txt file:

5+3=
5245+2=
76+0=
0-0+4=

All I want to do is log out positions of +.
I mean:

Line 1 position 2
Line 2 position 5
Line 3 position 3 
Line 4 position 4

I was trying with indexOf but it always showed me positions of + like the .txt file was in 1 line.

2,7,10,14

Sorry for bad English.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Shado
  • 1
  • Can you edit your question with a complete example? We need to see how you are reading the file. – sigmus Aug 21 '18 at 14:10

1 Answers1

0

You could put each line of the txt file into an array item and iterate through that array to find the index of each item. See: https://stackoverflow.com/a/6832105/4927000

Something like this:

var fs = require('fs');
var array = fs.readFileSync('[filename].txt').toString().split("\n");

for (i in array) {
    var index = i.indexOf('+');
    var line = i + 1;
    console.log('Line ' + line + ' position ' + index);
}
Josh
  • 444
  • 1
  • 7
  • 19