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?
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello".
The best way to declare a string literal in your code is to use array notation, like this: char string[] = "I am some sort of interesting string. \n"; This type of declaration is 100 percent okey-doke.
In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals.
If you are passing a string argument of 8-bit characters to such a component, declare it as Byte() , an array of Byte elements, instead of String in your new Visual Basic code. Type Characters. Appending the identifier type character $ to any identifier forces it to the String data type.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With