Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 891
CJ7 Avatar asked Oct 31 '12 10:10

CJ7


People also ask

What is verbatim string literal?

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

How do you declare a string literal?

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.

What is a verbatim string literal and why do we use it?

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.

How do you declare a string in Visual Basic?

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.


2 Answers

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. 
like image 136
Steve Avatar answered Sep 18 '22 07:09

Steve


@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" 
like image 24
JJS Avatar answered Sep 21 '22 07:09

JJS