Multiline string variable

You (initially) marked this as c#, but show VB code. For c#: use the @ specifier:

string myText =
@"line 1
line 2
line 3"

Note that if you don’t want a blank line at the start of your string, make sure you put the @" on the same line as the first line of your text, as I’ve done above.

For VB.NET, there is no direct support for this, but you can use the nice hack from this answer to get around it:

Dim s As String = <a>line 1
line 2
line 3</a>.Value

Also consider creating a string resource; you can add line breaks in there (ensure you use shift-enter, per the note in this answer), then load the resource using something similar to

Dim myString As String = My.Resources.MyString

Update for Visual Studio 2015: Obviously vb.net was the difficult case here, but as of VS2015 it supports multi-line strings in a fashion similar to c# verbatim strings, but without the preceding @.

Note that the line terminators embedded in the string are the actual line terminators provided by your editor of choice. For VS this is \r\n.

Example:

enter image description here

Source here.

For the new interpolated strings introduced in VS2015 / C# 6, prefix the string with $@ in C#:

string multiline = $@"
[configuration]
name=Fred
age={age}";

In VB.NET, just leave out the @:

Dim multiline As String = $"
[configuration]
name=Fred
age={age}"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)