![]() |
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 returnString 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
Output: string 1 2. 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
Output: Hello Everyone. |
Reffered: https://www.geeksforgeeks.org
Java |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |