none
Inserting a line break in a literal string

    Question

  • I am just getting started with VB and I am sort of used to QuickBasic, and I wanted to open a text file such as "C:/File.txt" and add to it.  I got the file open and all of its contents into a string, but I can't add line breaks to it.  The line breaks display fine in a MsgBox command but I can't insert the line break string (which looks like two vertical lines) into a literal string.  If I try to paste it into the VB Code window, it actually adds a line break just as if I had pressed Enter, and the symbols look different than the Shift + \ line ( | ).  The program looks sort of like this:

    FileContents = My.Computer.FileSystem.ReadAllText("C:\File.txt")
    Now, a Sub for a button
    NewData = TextBox1.Text
    FileContents = FileContents + The Line Break Character + NewData

    I need to add the NewData string to the end (bottom of .txt file) of the string FileContents, but I can't find a line break character or function.  Is there a function or special character that inserts a line break?  I have tried /n, \n, and the HTML <br> all with no success.
    Wednesday, July 12, 2006 1:49 AM

Answers

  • I think what you're looking for is the constant VbCrLf which stands for Visual Basic Carriage Return Line Feed. Its equivalent to the  C language's \n. Hope that helps

    Example

    Dim s as String = "Hello this is one line of text" & vbCrLf & "And this is on another."
    Wednesday, July 12, 2006 4:49 AM
  • Try:

    Environment.NewLine

    or:

    \r\n

    Wednesday, July 12, 2006 1:55 AM
    Moderator

All replies

  • Try:

    Environment.NewLine

    or:

    \r\n

    Wednesday, July 12, 2006 1:55 AM
    Moderator
  • Thanks for the help, much appreciated

    /r/n did not work but Environment.NewLine did
    Wednesday, July 12, 2006 3:13 AM
  • /r/n is different than \r\n

     

    ;)

    Wednesday, July 12, 2006 4:06 AM
    Moderator
  • I think what you're looking for is the constant VbCrLf which stands for Visual Basic Carriage Return Line Feed. Its equivalent to the  C language's \n. Hope that helps

    Example

    Dim s as String = "Hello this is one line of text" & vbCrLf & "And this is on another."
    Wednesday, July 12, 2006 4:49 AM
  • Hi,

    Look at http://www.lookuptables.com/

    for all the printable characters.

    If you want some that you can't get easily on the keyboard

    use CHR(number)

    E.G.

    mystring=Chr(124) & " the rest of my example string."

    Additionally with the line>>

    Imports Microsoft.VisualBasic.ControlChars

    at the very top of your code window you can then use the keyword

    NewLine as in>>

    TestWord = InputBox("Please input the word to " & _

    NewLine & "test as a palindrome")

     

    Regards,

    S_DS.

     

    Wednesday, July 12, 2006 1:29 PM
  • I tried \r\n too, like & "\r\n" and like & \r\n but neither worked, but Environment.NewLine works and vbCrLf both work.  Thanks for the help.
    Thursday, July 13, 2006 4:05 PM