Line break (“\n”) is a single character, to replace all line breaks from strings replace() function can be used.
String replace(): returns a new String object that contains the same sequence of characters as the original string, but with a given character replaced by another given character.
public String replace(char old,char new)
- old: old character
- new: new character
Returns value: Returns a string by replacing an old character with the new character
Original String with line breaks:Replace all line breaks from strings String after replacing line breaks:Replace all line breaks from strings
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.