[vb.net] New line character in VB.Net?

I am trying to print a message on a web page in vb.net. I am trying to get the messages in new lines. I tried using the "\r\n" and the new line character. But this is getting printed in the page instead of it comming to the next line. Please let me know if there is any alternative.

This question is related to vb.net string

The answer is


The proper way to do this in VB is to use on of the VB constants for newlines. The main three are

  • vbCrLf = "\r\n"
  • vbCr = "\r"
  • vbLf = "\n"

VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do. One of the reasons for doing this is ease of use when dealing with file paths.

  • C++ file path string: "c:\\foo\\bar.txt"
  • VB file path string: "c:\foo\bar.txt"
  • C# file path string: C++ way or @"c:\foo\bar.txt"

Environment.NewLine or vbCrLf or Constants.vbCrLf

More information about VB.NET new line:

http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx


Environment.NewLine is the most ".NET" way of getting the character, it will also emit a carriage return and line feed on Windows and just a carriage return in Unix if this is a concern for you.

However, you can also use the VB6 style vbCrLf or vbCr, giving a carriage return and line feed or just a carriage return respectively.


In this case, I can use vbNewLine, vbCrLf or "\r\n".


In asp.net for giving new line character in string you should use <br> .

For window base application Environment.NewLine will work fine.



If you are using something like this.

Response.Write("Hello \r\n")
Response.Write("World \r\n")

and the output is

Hello\r\nWorld\r\n

Then you are basically looking for something like this

Response.Write("Hello <br/>")
Response.Write("World <br/>")

This will output

Hello 
World

you can also just define "<br />" as constant and reuse it

eg.

Public Const HtmlNewLine as string ="<br />"
Response.Write("Hello " &  HtmlNewLine) 
Response.Write("World " &  HtmlNewLine)

you can solve that problem in visual basic .net without concatenating your text, you can use this as a return type of your overloaded Tostring:

System.Text.RegularExpressions.Regex.Unescape(String.format("FirstName:{0} \r\n LastName: {1}", "Nordanne", "Isahac"))

vbCrLf is a relic of Visual Basic 6 days. Though it works exactly the same as Environment.NewLine, it has only been kept to make the .NET api feel more familiar to VB6 developers switching.

You can call the String.Replace() function to avoid concatenation of many single string values.

MsgBox ("first line \n second line.".Replace("\n", Environment.NewLine))


it's :

vbnewline

for example

Msgbox ("Fst line" & vbnewline & "second line")


VbCr

Try that.


Your need to use the html/xhtml break character:

<br />

Check out Environment.NewLine. As for web pages, break lines with <br> or <p></p> tags.