Java Program to Replace All Line Breaks from Strings Last Updated : 28 Oct, 2021 Comments Improve Suggest changes Like Article Like Report Given a string, write a Java program to replace all line breaks from the given String. Examples 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. Syntax: public String replace(char old,char new) Parameters: old: old characternew: new character Returns value: Returns a string by replacing an old character with the new character Java // Java Program to Replace All // Line Breaks from Strings public class remove { public static void main(String[] args) { String s = "Replace\n" + " all\n" + " line\n" + " breaks\n" + " from\n" + " strings"; System.out.println( "Original String with line breaks - " + s); // replacing line breaks from string s = s.replace("\n", ""); System.out.println( "String after replacing line breaks - " + s); } } OutputOriginal String with line breaks - Replace all line breaks from strings String after replacing line breaks - Replace all line breaks from strings Comment More infoAdvertise with us Next Article Java Program to Replace All Line Breaks from Strings P poojavichare1810 Follow Improve Article Tags : Java Java Programs Java-Strings Practice Tags : JavaJava-Strings Similar Reads Java Program to Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 min read Java Program to Create String from Contents of a File A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be lear 6 min read Java Program to Print a New Line in String Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, that is how to print a new line in a string using Java. Metho 3 min read How to Replace All Occurings of String Using Regex in Java? Regex in Java is an interesting way to search for patterns in a string that the user provides. Expanded as Regular Expressions, It consists of some patterns that can be planned and modified according to the usage in the program. Example of Replace All Occurings of a StringInput: str="This is a sampl 2 min read How to Remove all White Spaces from a String in Java? In Java, to remove all white spaces from a String, there are several ways like using replaceAll() or manual iteration over characters. In this article, we will learn how to remove all white spaces from a string in Java.Example:We will use the replaceAll() method with the regex pattern "\\s" to remov 3 min read Like