I'm currently working on a project that involves tweeting excerpts from a book daily via a small app.
The book's content is stored in a text file and I need to split it into 140-character-long strings for posting.
Initially, I tried using the split() function but didn't achieve the desired outcome.
There isn't a specific separator between the strings I want to create.
One approach I considered was counting the characters in the text file and setting a limit (number of splits) to generate 140 character strings, but I feel there might be a more straightforward method.
Any suggestions?
Below is my current code, utilizing test.txt to access the book content in text form.
var fs = require('fs');
var array = fs.readFileSync('./test.txt').toString().match("{1,140}");
for(i in array) {
console.log(array[i]);
}
After implementing your advice, the console output seems unusual. Here's an updated version of my code:
var fs = require('fs');
var book = fs.readFileSync('./test.txt');
var lastSplit; // cache the position of the last split
var limit = book.length > 140 ? 140 : book.length - lastSplit;
var urlsToAdd = book.slice(lastSplit, lastSplit + limit);
for(i in book) {
console.log(book[i]);
}
Appreciate your help!