Multiline strings in VB.NET

pistacchio picture pistacchio · Apr 1, 2009 · Viewed 139.1k times · Source

Is there a way to have multiline strings in VB.NET like Python

a = """
multi
line
string
"""

or PHP?

$a = <<<END
multi
line
string
END;

Of course something that is not

"multi" & _
"line

Answer

Vincenzo Alcamo picture Vincenzo Alcamo · Nov 4, 2009

You can use XML Literals to achieve a similar effect:

Imports System.XML
Imports System.XML.Linq
Imports System.Core

Dim s As String = <a>Hello
World</a>.Value

Remember that if you have special characters, you should use a CDATA block:

Dim s As String = <![CDATA[Hello
World & Space]]>.Value

2015 UPDATE:

Multi-line string literals were introduced in Visual Basic 14 (in Visual Studio 2015). The above example can be now written as:

Dim s As String = "Hello
World & Space"

MSDN article isn't updated yet (as of 2015-08-01), so check some answers below for details.

Details are added to the Roslyn New-Language-Features-in-VB-14 Github repository.