66

How do you do a verbatim string literal in VB.NET?

This is achieved in C# as follows:

String str = @"c:\folder1\file1.txt";

This means that the backslashes are treated literally and not as escape characters.

How is this achieved in VB.NET?

CJ7
  • 22,579
  • 65
  • 193
  • 321

7 Answers7

75

All string literals in VB.NET are verbatim string literals. Simply write

Dim str As String = "c:\folder1\file1.txt"

VB.NET doesn't support inline control characters. So backslashes are always interpreted literally.

The only character that needs to be escaped is the double quotation mark, which is escaped by doubling it, as you do in C#

Dim s As String = """Ahoy!"" cried the captain." ' "Ahoy!" cried the captain.
SSS
  • 4,807
  • 1
  • 23
  • 44
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    There is a very good reason. Line breaks. Doing `var string = @"SELECT * FROM myTable WHERE etc";` in C# is much cleaner than 'Dim query As String = "SELECT * " + _ "FROM myTable " + _ "WHERE something"` – nathanchere Jan 17 '14 at 04:52
  • 3
    While I concur with your conclusion on clarity, this is not the point. You are confusing Line Continuation (_) with string concatenation (+ or better &), I have said that VB.NET doesn't support inline control characters, so there is no need to support for special characters (@) that change the meaning of one or more of the internal string chars. IN c# you need @ to change the meaning of a backslash to its face value instead of the escaping functionality allowed by the language. – Steve Jan 17 '14 at 08:17
  • 7
    No, I'm really not confusing them at all. It's YOUR point, ie "There is no reason for an equivalent to C# verbatim string literals in VB.NET because it doesn't support inline control characters". A verbatim string in C# allows you to have a multi-line string value declared inline without concatenation. VB's lack of support for this means concatenation is unavoidable without instead doing something like storing the string as a resource. That's a reason why a VB equivalent would be useful, ie it's not just about escaping special characters. – nathanchere Jan 20 '14 at 00:25
  • those two strings are different. The C# version has newlines and copious whitespace. – JJS Jan 06 '16 at 03:19
  • 1
    In this particular example with a file path it might be better to use Path.DirectorySeparatorChar in any case. – phn Jun 27 '18 at 10:10
  • 1
    @nathanchere Since 2015 VB .NET does literal newline (but SO comments don't haha) `Dim s As String = "SELECT *` `FROM mytable"` VB also does implicit line continuation for append `Dim s As String = "SELECT * " &` `"FROM mytable"` Agree with Steve that no need for verbatim marker, since strings in VB are verbatim by default. More a need for an interpolation marker, which there is now in $ https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/interpolated-strings – Jason S May 11 '20 at 14:18
  • > VB.NET doesn't support inline control characters. Worth noting that it does. With the $ prefix. – Paul Hutchinson Oct 28 '21 at 16:08
33

@MarkJ already pointed this out in @Jon Skeet's post.

VB.Net supports this abomination feature, if you absolutely need to use a verbatim via an inline XML Literal.

Consider Caching the String! Don't evaluate this every time...

Imports System.Xml.Linq

Dim cmdText as String = <![CDATA[
SELECT 
Field1, Field2, Field3 
FROM table
WHERE Field1 = 1
]]>.Value

[edit 2015-Jan-5]

VB14 / VS2015 supports multi-line strings without any shenanigans.

Dim cmdText as String = "
SELECT 
Field1, Field2, Field3 
FROM table
WHERE Field1 = 1"
JJS
  • 6,431
  • 1
  • 54
  • 70
  • Inline XML literals is something VB has that I can't believe hasn't been added to C# yet. That and optional refs. – Brain2000 Jun 02 '22 at 15:55
12

VB doesn't treat \ as an escape character anyway, so you can just write the string as a normal literal:

Dim str = "c:\folder1\file1.txt"

As far as I'm aware, VB doesn't have any way of achieving the other major goal of verbatim string literals, that of allowing multiple lines - you have to use VbCrLf for that, I believe. (Or Environment.NewLine of course - it depends on your requirements. Sometimes you want the system-specific line separator; sometimes you want a specific one as required by a particular protocol etc.)

EDIT: Newer versions of VB support multiple lines in string literals

SSS
  • 4,807
  • 1
  • 23
  • 44
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @Pondidum: Depending on the requirements, of course. Will edit. – Jon Skeet Oct 31 '12 at 10:38
  • 1
    +1 Multiple line string literals aren't supported in VB.Net. Some people use (or abuse?) XML literals for a similar effect like [this](http://stackoverflow.com/a/102321/15639) (see the comment). – MarkJ Oct 31 '12 at 12:35
5

When in doubt look at this comparison page: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

VB.NET

 'No string literal operator     
 Dim filename As String = "c:\temp\x.dat"

C#

// String literal 
string filename = @"c:\temp\x.dat";  
string filename = "c:\\temp\\x.dat";  
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25
2

VB.NET do not recognize "\" as an escape character. But, maybe you may use further solution (take into account, that it's works slowly than concatenation, e.g.):

Dim s As String = Regex.Unescape("c:\\folder1\\file1.txt\nc:\\folder1\\file2.txt\nc:\\folder1\\file3.txt")

In this case, string "s" contains three lines. Symbol "\" is protect next "\" from regex method Unescape(), that's why it repeat twice each time.

"\n" is a C#-like "new line" special character. You also may use "\t" (tab), and so on.

Aave
  • 548
  • 1
  • 5
  • 15
2
Dim sourceText As String =  
    <string>  


    Imports Microsoft.VisualBasic  
    Imports System  
    Imports System.Collections  
    Imports Microsoft.Win32  
    Imports System.Linq  
    Imports System.Text  
    Imports Roslyn.Compilers  
    Imports System.ComponentModel  
    Imports System.Runtime.CompilerServices  
    Imports Roslyn.Compilers.VisualBasic  

    Namespace HelloWorld  
      Module Program  
        Sub Main(args As String())  
          Console.WriteLine("Hello, World!")  
        End Sub  
      End Module  
    End Namespace  
</string>  
0

VB XML literals unfortunately will not work in a .vbhtml razor page. Hopefully that will change in the next release.

Randall
  • 41
  • 3