+ 1

Why we use \r in java? , if i want donot writting some thing I do not write that.Why should we use "t\rh" instead of "h"?

12th Aug 2018, 8:12 AM
alireza
7 Answers
+ 5
I need to see the code before I can say what, however, if the \r is written in a string it represents a carriage return character, on some systems they serve the purpose of inserting a line break, so the text following \r will be written on a new line.
12th Aug 2018, 10:39 AM
Ipang
+ 3
Sorry for the delay, I'm on a phone, and I type slow on phone : ) // Printing words on separate line // using escape character for line break public class Sample { public static void main(String[] args) { // \n is used for line break on Android // Linux, and OS X System.out.print("'Linux\nOS X\nAndroid'"); System.out.println(); // \r is used for line break on Mac OS System.out.print("'Mac\rOS'"); System.out.println(); // \r\n is used for line break on // Microsoft Windows System.out.print("'Microsoft\r\nWindows'"); } }
12th Aug 2018, 11:06 AM
Ipang
+ 2
alireza I guess Notepad++ is probably equipped with automatic line break detection mechanism, as it is an advanced text editor, it's smart enough to adjust its view accordingly. Windows standard text editor like Notepad will be the way : ) If you have a hex editing program you can also verify this easily, create a text file, write a few lines, then open it in hex editor, there you can see where a line break occurs there would be a carriage return \r followed immediately by a new line \n, these are 0d 0a respectively, in hex view.
12th Aug 2018, 1:55 PM
Ipang
+ 1
Ipang can you show me the code that uses \r with necessry function for \r
12th Aug 2018, 11:05 AM
alireza
+ 1
Ipang thanks for your answer. In the last print method when I remove \r, we donot have any changes and in macos it just does the work of space key for us ?
12th Aug 2018, 11:21 AM
alireza
+ 1
alireza Yes, but output really depends on where you test each of these cases, although in general \n is more common being used for line break. The last print showed the line break for Windows, if you remove \r the line will not break, this will be obvious if you write the string into a file, and open it in a standard text editor e.g. Notepad, but it may not be obvious on a console : ) P.S. I don't have a Mac OS machine, so I can't tell for sure, I understand about the difference of system line breaks from an article I read before.
12th Aug 2018, 11:29 AM
Ipang
+ 1
Ipang Oh thanks.I should test it in a text editor later.thank you very much. Is note pad++ suitable for testing it?
12th Aug 2018, 11:40 AM
alireza