I'm struggling to understand the difference between \n and \r in swift specifically. I read that \r returns the cursor to the beginning of the same line and the links being shared to me as answers contradict what swift is doing. I tested this here and expected to get "worldhello". If i replace \r with \n, I get the same result so i'm not sure when to use which
Asked
Active
Viewed 327 times
0

ajc
- 13
- 2
-
Does this answer your question? [What are carriage return, linefeed, and form feed?](https://stackoverflow.com/questions/3091524/what-are-carriage-return-linefeed-and-form-feed) – Mohamed AMAZIRH Aug 01 '21 at 00:38
-
Each operating system defines its own way of recongnizing a carriage return. – El Tomato Aug 01 '21 at 00:40
-
@aheze Not really.. those are saying that it returns the cursor on the same line and it's not doing that in the screenshot i posted – ajc Aug 01 '21 at 00:49
-
@MohamedAMAZIRH Not really.. those are saying that it returns the cursor on the same line and it's not doing that in the screenshot i posted – ajc Aug 01 '21 at 00:50
-
i'm still not sure when to use which – ajc Aug 01 '21 at 00:50
-
You are right in that '\r' should return the cursor to the beginning of the current line. But in the answer I linked, they mention that control characters may be interpreted differently depending on the OS (and also the console you're using). The issue you're seeing is not specific to swift but to the console used by your IDE to output the result. Apparently it's treating `\r` as `\n`. Try launching your program directly from a terminal (without the ide) and see if you have a different result. – Mohamed AMAZIRH Aug 01 '21 at 11:37