carriage return in Java Last Updated : 24 Oct, 2023 Comments Improve Suggest changes Like Article Like Report In Java, carriage return is represented as the escape sequence \r and is used to move the cursor to the beginning of the current line without continuing to next line. When combined with \n it can be used to create a new line and move the cursor to the beginning of that new line and a combination of \r and \n is used to represent a standard newline on Windows-based systems. Syntax of Java carriage return String text = "Hello\rWorld!";Parameters:The string text will contain the value "text1" followed by carriage return and then "text2". Example of Java Carriage return1. Carriage Return in a StringIn this example, we have a single string with a carriage return \r and which moves the cursor back to the beginning of the line before printing "string 2". Below is the implementation of the above method: Java // Java Program to demonstrate // Carriage Return in a String import java.io.*; // Driver Class public class Example01 { // main function public static void main(String[] args) { String text = "string 1\rstring 2"; System.out.println(text); } } Output: string 1string 22. Replacing Newline with Carriage ReturnIn this example, we use the replaceAll() method to replace all occurrences of \n with \r\n and This allows us to convert text with newline characters into text with the carriage return and line feed suitable for the different platforms or file formats. Below is the implementation of the above method: Java // Java Program to demonstrate // Replacing Newline with Carriage Return import java.io.*; // Driver Class public class Example02 { // main function public static void main(String[] args) { String text = "Hello Everyone.\nwelcome to GeeksforGeeks.\n"; String result = text.replaceAll("\n", "\r\n"); System.out.println(result); } } Output: Hello Everyone.welcome to GeeksforGeeks. Comment More infoAdvertise with us Next Article carriage return in Java M mguru4c05q Follow Improve Article Tags : Java java-basics Practice Tags : Java Similar Reads String Class in Java A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl 7 min read Character Array in Java In Java, a character array is a data structure used to store a sequence of characters. The characters are stored in contiguous memory locations and can be accessed by their index, similar to an array of integers or any other data type. Declaring a Character Array A character array can be declared in 5 min read Java CharArrayWriter Class | Set 2 In the Java.io.CharArrayWriter class in Java | Set 1, we have already discussed about which CharArrayWriter class and how it works. In this article, we are going to discuss some more methods of the CharArrayWriter class, which give us strong control over handling character data. Java CharArrayWriter 3 min read Java.io.Writer class in Java This abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both. 4 min read Java CharArrayWriter Class | Set 1 The CharArrayWriter class in Java is part of the java.io package, and it is used to write data into a character array. This class is used when we want to store characters temporarily without dealing with files and other storage. Features of CharArrayWriter Class:,It is used to store characters in me 5 min read Like