67

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?

7 Answers 7

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.
7
  • 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"` Jan 17, 2014 at 4: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, 2014 at 8: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. Jan 20, 2014 at 0:25
  • 1
    In this particular example with a file path it might be better to use Path.DirectorySeparatorChar in any case.
    – phn
    Jun 27, 2018 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 $ learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/…
    – Jason S
    May 11, 2020 at 14:18
34

@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"
2
  • 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 2, 2022 at 15:55
  • Every time an upvote happens, it makes me glad I'm not working at the job where we switched between vb.net and c#. vb.net is a fine language, but I don't miss it...
    – JJS
    Oct 5 at 13:21
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

2
  • 1
    @Pondidum: Depending on the requirements, of course. Will edit.
    – Jon Skeet
    Oct 31, 2012 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 (see the comment).
    – MarkJ
    Oct 31, 2012 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";  
0
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.

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>  
1
  • Why the downvote? This has been a great solution pre VB14
    – Mo Gauvin
    Oct 11, 2018 at 14:55
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.