Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are \r \t and \n different from one another? [duplicate]

In the code below, I don't know how these characters are different functionally from one another: \r \t \n. Does anyone have an explanation or description for these?

Here is some sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Sorting words in a block of text by length</title>
        <link rel="stylesheet" type="text/css" href="common.css" />
    </head>
    <body>
        <h1>Sorting words in a block of text by length</h1>

<?php

$myText = <<<END_TEXT
But think not that this famous town has 
only harpooneers, cannibals, and 
bumpkins to show her visitors. Not at 
all. Still New Bedford is a queer place. 
Had it not been for us whalemen, that 
tract of land would this day perhaps 
have been in as howling condition as the 
coast of Labrador.
END_TEXT;

echo "<h2>The text:</h2>";
echo "<div style=\"width: 30em;\">$myText</div>";

$myText = preg_replace( "/[\,\.]/", "", $myText );
$words = array_unique( preg_split( "/[ \n\r\t]+/", $myText ) );
usort( $words, create_function( '$a, $b', 'return strlen($a) - strlen($b);
' ) );

echo "<h2>The sorted words:</h2>";
echo "<div style=\"width: 30em;\">";

foreach ( $words as $word ) {
    echo "$word ";
}
echo "</div>";

?>
    </body>
</html>
like image 753
Preston blah Avatar asked Mar 15 '13 01:03

Preston blah


People also ask

Are \r and \n the same?

The \r moves to the current line's right, without moving to the next line while \n will move to the start of the next line .

What does \r and \n mean?

\n means new line. It means that the cursor must go to the next line. \r means carriage return. It means that the cursor should go back to the beginning of the line.

What is the difference between \n and \r in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

What does \t do in r?

t() function in R Language is used to calculate transpose of a matrix or Data Frame.


2 Answers

\n is a symbol for new line

\t is a symbol for tab

and \r is for 'return'

You can find more information here: What is the difference between \r and \n?

like image 174
Lemurr Avatar answered Sep 21 '22 19:09

Lemurr


The \n symbol means literally new line. This will go to the start of the next new line.

The \t symbol means add a tab (which is usually 4 spaces but can easily be 2 or 8 depending on the context).

The \r symbol is no more used that often. It means carriage return which means go to the start of the line. It was used together with \n for being sure even "old" printers would get to the beginning of the next line.

like image 41
Shoe Avatar answered Sep 19 '22 19:09

Shoe