0

I made a .txt file (UTF-8) and put the following text in it:

123
456
789

I then tried to find out the position of the character '4' by using the InStr() function. Surprisingly, the result was 6. I tried with the character '3', and the result was 3. So, there must be two characters in between the 3 and the 4.

I then tried InStr(TextBox1.Text, Chr(13)) and the result was 4.

Ok. The "New Line" has a character located in the 4th position. If so, then what is 5th character in there?

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
K.JH
  • 21
  • 4
  • 2
    A new line on Windows is a carriage return (CR, Chr(13)) followed by a line feed (LF, Chr(10)). So a new line is CRLF, or vbCRLF. – Ken White Jan 17 '19 at 20:03
  • This goes back to olden days of typewriters. You would push a lever that would move the entire carriage back to beginning of the line and advance the paper one line. Amazingly it could do both at once. – Mary Jan 17 '19 at 20:30
  • 1
    Possible duplicate of [Differences Between vbLf, vbCrLf & vbCr Constants](https://stackoverflow.com/questions/27223228/differences-between-vblf-vbcrlf-vbcr-constants) – 41686d6564 stands w. Palestine Jan 17 '19 at 23:05

1 Answers1

4

The actual character(s) used for new-line are different from one platform to the next. Some systems use character 13 to mean a new-line, some use 10, and some, like Windows, use both.

Technically, character 13 is supposed to mean a carriage return (return the print head of the printer to the left side of the page), that's why it's often referred to as CR. Remember, in the early days of computing, printers were used as terminals rather than video screens. So, the closest equivalent of CR on a video terminal is for the cursor to return to the beginning of the current line, but not advance to the next line.

Character 10 means line-feed (LF), which means to advance (i.e. feed) the paper by one line so that the print head is ready to print on the next line. The closest equivalent to a line-feed on a video terminal is to move the cursor down to the next line, but keep it at the same x-position.

So, with those two-things in mind, if you wanted to begin typing at the beginning of the next line, you would need to do both things. You need to advance to the next line on the page AND return to the beginning of the line, hence CRLF, two characters.

Presumably, some system designers thought it was too wasteful to use two characters for each new-line, when the added nuance was rarely needed for computers with video displays, so they opted to only use either CR or LF. Since the correct encoding to use changes from one platform to another, it's best to use Environment.NewLine in .NET to get whichever is appropriate for the current system.

Consider, for example, when you run this console app on windows:

Public Sub Main()
    Console.Write("123" & vbCr)
    Console.Write("4" & vbCr)
End Sub

The output is:

423

The carriage-return only caused the cursor to go back to the beginning of the line. It didn't cause it to move down to the next line, so the 4, when it's printed, overwrites the 1.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105