stringvb.net

Multiline strings in VB.NET


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

Solution

  • Multi-line string literals were introduced in Visual Basic 14 (in Visual Studio 2015).

    Dim s As String = "Hello
    World & Space"
    

    For earlier versions of VB, 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