简体   繁体   English

在Javascript中加载文本文件会添加意外的额外隐藏字符

[英]Loading text file in Javascript adds unexpected extra hidden characters

Maybe it's just the extreme lack of sleep, but I can't figure out why this is happening. 也许仅仅是极端的睡眠不足,但我不知道为什么会这样。 I have a text file of 4 letter words, one on each line as follows: 我有一个包含4个字母的单词的文本文件,每行一个,如下所示:

text
lolz
test
word

When I try to load this file into an array line by line, I get strings that are 5 characters long! 当我尝试将此文件逐行加载到数组中时,得到的字符串长度为5个字符!

var request = new XMLHttpRequest();
request.open("GET", "test.txt");
request.onloadend = function()
{
    var wordList = request.response;
    wordList = wordList.split('\n');

    console.log(wordList[2].length);
}
request.send();

I'm using Notepad++ to edit, and I see that there are no spaces at the end of each line. 我正在使用Notepad ++进行编辑,并且看到每行末尾没有空格。 The sting.split() function is supposed to remove the characters the string is split by, so the extra characters shouldn't be '\\n'. sting.split()函数应该删除字符串被分割为的字符,因此多余的字符不应为'\\ n'。 Using the console.log in Chrome, I can't determine what the extra character actually is. 使用Chrome中的console.log,我无法确定多余的字符实际上是什么。 There appears to be none! 似乎没有!

This can be an odd problem if you've never run into it before. 如果您以前从未遇到过,这可能是一个奇怪的问题。 Sometimes not only an '\\n' character is included for a new line (which you seem to be familiar with already) but an '\\r' character for a "carriage return" as well. 有时,不仅在换行符中添加了“ \\ n”字符(您似乎已经很熟悉),而且在“回车符”中还包含了“ \\ r”字符。 This is meant to represent how typewriters would need to move all the way back to the left, in addition to skipping down a space, to begin a new line. 这是为了表示打字机除了跳过空格外,还需要如何一直移回左侧以开始新的一行。 I'm not sure you can predict when you'll get carriage returns... It could be platform specific? 我不确定您是否可以预测何时获得回车……可能是特定于平台的吗? Or maybe text editor specific? 还是特定于文本编辑器? Perhaps someone with real knowledge can expand this answer. 也许真正有知识的人可以扩展这个答案。

Anyway, for now, try adding a trim() to each of your entries, which should get rid of the hidden '\\r' that you can't see, as follows: 无论如何,现在,请尝试为每个条目添加trim(),这将摆脱看不见的隐藏“ \\ r”,如下所示:

var request = new XMLHttpRequest();
request.open("GET", "test.txt");
request.onloadend = function()
{
    var wordList = request.response;
    wordList = wordList.split('\n');

    for(var i = 0; i < wordList.length; i++)
        wordList[i] = wordList[i].trim();

    console.log(wordList[4].length);
}
request.send();

I had the same sort of problem when splitting a date string; 分割日期字符串时,我遇到了同样的问题; an extra 'non-visible' character was added to the beginning and end of each array item; 在每个数组项的开头和结尾处添加了一个额外的“不可见”字符; string.trim() did not work. string.trim()无法正常工作。

Took me 2 days to work the following out: 我花了2天的时间进行以下工作:

Created the datetime array from: 从以下位置创建了datetime数组:

        var datetime = new Date();
        var dt = datetime.toLocaleString();
        var datetimearray=dt.split(" ");

Created date and time strings from the datetime array: 从datetime数组创建的日期和时间字符串:

        var date=datetimearray[0];
        var time=datetimearray[1];

Split the date and time strings: 分割日期和时间字符串:

        var datearray=datetimearray[0].split("/");
        var timearray=datetimearray[1].split(":");

Then converted each date and time element to a number: 然后将每个日期和时间元素转换为数字:

        var day =  getNumberFromString(datearray[0]);
        var month= getNumberFromString(datearray[1]);
        var year = getNumberFromString(datearray[2]);
        var hour =  getNumberFromString(timearray[0]);
        var minute= getNumberFromString(timearray[1]);
        var second = getNumberFromString(timearray[2]);

The getNumberFromString function: getNumberFromString函数:

    function getNumberFromString(string)
    {
        var stringsplit=string.split("");
        var stringlen=string.length;  // If it is the year part of the date, the length is 6
        switch(stringlen)
        {
             case 3:  // The last part of the time element (seconds) is only 3 for some reason
             case 4:  // The two character elements (day, month, hour, minute) are length 4 after the split
               var string1=stringsplit[1];  // create the first needed character from second array element; index 1
               var string2=stringsplit[2];  // create the second needed character from third array element; index 2

               string=string1+string2;  //add the 2 new elements together
               return parseInt(string); //convert the number as a string to a number

            case 6:  // For the date string; similar to the above 
              string1=stringsplit[1];
              string2=stringsplit[2];
              string3=stringsplit[3];
              string4=stringsplit[4];
              year1=string1+string2;
              year1num=parseInt(year1);
              year2=string3+string4;
              year2num=parseInt(year2);
              return [year1,year2];            
        }
    }

Finally created a lookup array to use for some encryption: 最后创建了一个用于加密的查找数组:

    var lookup=[day,month,year1,year2,hour,minute,second];

If anyone can show me a shorter way, I would be grateful. 如果有人可以向我展示一个简短的方法,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM