To clear a line break from a file

Asked 1 years ago, Updated 1 years ago, 97 views

How do I get rid of all line breaks when I read a text file in Java?

String text = readFileAsString("textfile.txt");
text.replace("\n", "");

I tried it like this, but it didn't work.

string java newline line-breaks

2022-09-22 22:13

1 Answers

First of all, you have to substitute the result of text.replace into text again.

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");

This is how it should be done.


2022-09-22 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.