Copy link to clipboard
Copied
I have a little problem with importing data from a text file.
I have a file with exported data from documents. Each line in the file consists of a field name, a separator (here, for example, "::") and the contents of the field.
I point to the file with
var rStream = util.readFileIntoStream();
I read its content
var cFile = util.stringFromStream(rStream, "utf-16");
The file is saved with UTF-16 encoding.
Then there is the processing of the read text line by line.
I split the file into lines using .split('\r\n') and then the lines into fields using .split('::') And here comes the problem, all lines of data except the first load fine.
The first line contains the text:
Organ_Nazwa::Urzad Miasta
However, for the script, the beginning of this line is different and is not the same as the "Organ_Nazwa" string, even though it is visible. However, I just add a blank line at the beginning of the file and everything is OK.
Everything looks exactly the same in the debugger, but it's not. It looks as if this string from the beginning of the file contains some white space at the beginning.
Copy link to clipboard
Copied
I dealt with this issue in the past so I'll save you a lot of headaches and provide the solution. Many text files contain something called a BOM (Byte Order Mark) which is a zero-width unicode character. The reason for it is not important, but it's always the first character in the file, and needs to be removed manually if you want the text in the first line to match your field names, for example.
To do so use the following command:
cFile = cFile.replace("\uFEFF", "");
Copy link to clipboard
Copied
I dealt with this issue in the past so I'll save you a lot of headaches and provide the solution. Many text files contain something called a BOM (Byte Order Mark) which is a zero-width unicode character. The reason for it is not important, but it's always the first character in the file, and needs to be removed manually if you want the text in the first line to match your field names, for example.
To do so use the following command:
cFile = cFile.replace("\uFEFF", "");
Copy link to clipboard
Copied
Thanks for the answer, the .trim() function also gives the right result.
Copy link to clipboard
Copied
Yes, but be aware it will not work in older versions of Acrobat or Reader.

