search

Home  >  Q&A  >  body text

JavaScript's split() method automatically adds the \r character at the end of the string

<p>I'm trying to create an array with the fetched data and create a new collection with it, but the resulting string has \r at the end: </p> <pre class="brush:php;toolbar:false;">export const WordSetFn = async () =>{ let wordSet; await fetch(wordsSet).then((resp) => resp.text() ).then((resp) =>{ const wordSetArray = resp.split("\n") wordSet = new Set(wordSetArray) }) return {wordSet}; } // word set would look like this: {"above\r",...}</pre> <p>And the txt file I'm getting the data from is a bunch of words, each word is on the next line, like this: </p> <pre class="brush:php;toolbar:false;">aback abase abate abbey abbot Abhor Abide abled Abode abort ...</pre> <p>Now why is \r added after every word? What does \r mean? </p>
P粉738248522P粉738248522443 days ago586

reply all(1)I'll reply

  • P粉066725148

    P粉0667251482023-09-04 11:11:05

    \r is a carriage return character, \n is a line feed character, \r\n is a carriage return character plus a line feed character.

    reply
    0
  • Cancelreply