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

随声附和 提交于 2019-11-28 09:34:54
Steve

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.

@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"
Jon Skeet

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

Carlos Quintanilla

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";  

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.

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

qianzuijiuren
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>  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!