Java Program to Replace All Line Breaks from Strings
Given a string, write a Java program to replace all line breaks from the given String.
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.
Input - Geeks For Geeks Output - Geeks For Geeks
Line Break: A line break (“\n”) is a single character that defines the line change.
In order to replace all line breaks from strings replace() function can be used.
String replace(): This method 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