I have an mbox file with 7 messages in it. I try to parse it with the following code on the string “From -“. The resulting object is of length 8 even though there are only 7 occurrences of the string. Even though the first occurrence of the string is at the first space on the first line, the parsing seems to include something before that and a print out of the value of the first element is ‘0’ (the remaining elements are the appropriate strings). Why is there an extra element (is it my code of the methods behaviour)?
var fs = require('fs');
// Read in the text file
fs.readFile( '../Data/Inbox 6Msg Mbox.txt', function (err, data) {
if (err) {
throw err;
}
// Convert file to a string object
var unParsed = data.toString();
// Split the text into an array of strings where each
// element is a message
var indivMessages = unParsed.split("From -");
console.log(indivMessages.length);
});
The mistake you’re making is known as a fencepost error. Although you have seven occurrences of the string, this will result in not seven but eight parts in between the occurrences of the string.
For instance:
Returns
even though the pipe only occurs twice
Even if I have the pipe as the first character, the resulting array is the same length:
returns
The logic is the same, but now
''has replaced'1','2', and'3'