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
  • 34,719
  • 24
  • 185
  • 251
derfium
  • 175
  • 1
  • 3
  • 15

2 Answers2

6

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
  • 168,929
  • 32
  • 262
  • 294
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", "");