7

Possible Duplicate:
Multiline strings in VB.NET

In c#, you can be all like:

string s = @"hello
there
mister";

Does VB.NET have something similar that doesn't involve string concatenation? I'd like to be able to paste multi-line text in between two double quotes. Somehow, I don't believe VB.NET supports this.

Community
  • 1
  • 1
oscilatingcretin
  • 9,987
  • 34
  • 115
  • 197

4 Answers4

15

EDIT: VS2015 ONWARDS

YOU CAN NOW HAVE MULTILINE STRINGS IN VS2015 BY WRITING THEM LIKE SO:

Dim text as String = "
This
Is
Multiline
Text!"

There is no multi-line string literal in VB .NET - the closest thing you can get (without using LINQ) is a multi-line statement with concatenation.

Prior to VS2010:

Dim x = "some string" & vbCrlf & _
        "the rest of the string"

Post 2010:

Dim x = "some string" & vbCrlf &
        "the rest of the string"

The XML/LINQ trick is:

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

Dim x As String = <a>Some code
and stuff</a>.Value

But this limits what characters you can place inside the <a></a> block because of XML semantics. If you need to use special characters wrap the text in a standard CDATA block:

Dim x As String = <a><![CDATA[Some code
& stuff]]></a>.Value
brandito
  • 677
  • 8
  • 19
Matt Razza
  • 3,446
  • 2
  • 24
  • 29
5

No, but you can use a xml trick like this:

Dim s As String = <a>hello
there
mister</a>.Value

or put your string in a project resource.

T.S.
  • 15,962
  • 10
  • 52
  • 71
user1598203
  • 93
  • 1
  • 6
2

I dont know if it's the best way of doing this but I don't think there's an equivalent operator.

Dim myString As String =
"Hello" & Environment.NewLine & "there" & Environment.NewLine & "mister"

I think the Environement.NewLine takes the correct line feed, depending on the OS.

EDIT: I've just read that you want to insert text multiline directly in the code, so there's another possible solution:

You have to use string quotations still, and commas, but here it is

    Dim myList as new List(of String) (new String(){
        "Hello",
        "there",
        "mister"
    })

    Dim result as String

    For Each bob as String In myList
        result += bob & Environment.NewLine
    next
Pacane
  • 18,736
  • 16
  • 55
  • 90
  • Unfortunately, this solution still doesn't satisfy my initial request of `I'd like to be able to paste multi-line text in between two double quotes`. To bring this string array together again, I would still have to rely on concatenation in some form, be it joining the array at `VbCrLf` or using `&=` in a loop. Thanks, though. – oscilatingcretin Aug 14 '12 at 17:56
0

This is what MSDN recommends http://msdn.microsoft.com/en-us/library/5chcthbw(v=vs.80).aspx

MyString = "This is the first line of my string." & VbCrLf & _ "This is the second line of my string." & VbCrLf & _ "This is the third line of my string."

amdmax
  • 761
  • 3
  • 14