Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VB.NET have a multi-line string declaration syntax equivalent to c#? [duplicate]

Tags:

c#

vb.net

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.

like image 249
oscilatingcretin Avatar asked Aug 14 '12 14:08

oscilatingcretin


People also ask

How do I split a string into multiple lines in VB net?

You can specify more than one delimiter. For example, dim result = inputString. Split({vbCrLf, vbLf, vbCr}, StringSplitOptions. RemoveEmptyEntries) will split the input string using \r\n , \n and \r , returning an array of strings.

Can you have multiple lines in a string?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.


2 Answers

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
like image 131
Matt Razza Avatar answered Oct 04 '22 04:10

Matt Razza


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.

like image 32
user1598203 Avatar answered Oct 04 '22 04:10

user1598203