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.
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.
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", "")
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", "");