Answer the question
In order to leave comments, you need to log in
Is there a universal way to put the lines of a text file into an array?
There is a text file
one
two
three
chetire
five
etc
fs.appendFileSync(Users, `${os.EOL}${text}`);//Users - файл, text - новая строка os.EOL - см.ниже
\r\n
, but in ubuntu \n
. Because of this, I'm having trouble creating an array from the lines of a file. let fileContent = fs.readFileSync(Users, "utf8"); //Users - файл
var sList = fileContent.split(os.EOL); //os.EOL - возвращает символ переноса строки, характерный для текущей операционной системы. см. выше
console.log(sList);
//sList будет в данном случае будет содержать
[
'one\r',
'two\r',
'three\r',
'chetire',
'five',
'etc'
]
//При этом
console.log(sList[0]);
//Вернет в консоль one без \r
\r
with
but it didn't work.
The problem is that sList.indexOf('one'); will return -1 due to \r, although in fact it should return 0
Of course, it would be possible to use the transfer when adding lines in the script and not take a steam bath, but I think that this is only a superficial fix, and if, for example, you need to manually edit the file on Linux, there will be a similar problem.
How can the script be universalized so that it works for files from both Windows and Linux? item.replace(/\r?\n/g, "")
\r\n
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question