views:

253

answers:

2

In C# you can use \ to ignore the special characters:

string myString = "this is a \" string";

that would work as one complete string... in VB, doing that does not work...

Anyone know the equivalent of \ to ignore special characters for VB?

+11  A: 

VB.NET doubles up the quotes like this:

Dim myString As String = "this is a "" string"
Andrew Hare
+4  A: 

For the quotation, double the quote:

"This is a ""quote"""

For everything else, you're out of luck and have to resort to Chr

"This is a string with a " & Chr(10) & "line-feed"
erikkallen
Wow. What on earth was the thinking behind this?
Beska
that's not true... you can use `vbCrLf`
Jason
Jason - you're right but you meant vbLf for Chr(10) :)
MarkJ
Check out http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.controlchars_members.aspx for a list of all available control characters in VB.
Partha Choudhury
The thinking is fairly obvious - when escape sequences exist, a person who's not aware of them will get confusing results if he writes a string literal that inadvertently includes one. This typically happens with path - how many questions can you remember about C# when people write "C:\foo.txt", and then complain about getting FileNotFoundException when it's clearly there etc... VB approach may be more verbose, but it's definitely more newbie-friendly. And it's consistent with traditional BASIC syntax, which never had escape sequences in string literals.
Pavel Minaev
System.Environment.Newline can be useful in this particular case.
Greg D