2

I have a .txt file form which I read multiple lines and append an array with every line. Unfortunately I also have the line breaks in the array.

When I try to replace them with line.replace("\n", ""), nothing will happen.

Cœur
  • 37,241
  • 25
  • 195
  • 267
derfium
  • 173
  • 1
  • 3
  • 15

2 Answers2

7

Are you just doing line.replace("\n", "")? If so, that's the problem. You are doing the replacement, then throwing away the result. You need:

line = line.replace("\n", "")
kindall
  • 178,883
  • 35
  • 278
  • 309
0

I had the same problem, but even saving the result in another variable. I started breaking it in code units to find the problem and I found that my input had carriage returns, which is '\r'. They made visually the same result as the '\n' in the output file. So I fixed it by doing the following:

result = input.replace("\n", "").replace("\r", "");