16

In following code, the usage of the string "“" (i.e. a left double quotation mark inside a string) results in a compile error in VB.NET:

StringVar = Replace(StringVar, "“", "“")

What’s going on here?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
zanhtet
  • 2,040
  • 7
  • 33
  • 58

3 Answers3

16

It seems as if you want to replace curly quotes with their HTML code equivalent.

On the first glance, your code is absolutely correct. The problem is that VB allows curly quotes in place of regular quotes in code (because Unicode is great, right?). That is, the following codes are all equivalent:

Dim str = "hello"
Dim str = “hello”
Dim str = "hello“

Now, if you want to use a quotation mark inside a string, VB doesn’t know whether the quotation mark is supposed to end the string or not. In C#, this would be fixed by escaping the quotation mark, i.e. in place of """ you’d write "\"". In VB, the same is done by doubling the quotation mark, i.e. """".

Back to your curly quote. The same as for straight quotes applies according to the VB language specification (¶1.6.4). So to write a curly quote in code, try the following:

StringVar = Replace(StringVar, "““", "“")

Unfortunately, I cannot try this code now and it’s altogether possible that the IDE simply replaces this by straight quotes. If that’s the case, an alternative is to use Chr or ChrW with the character code of the “left double quotation mark”:

StringVar = Replace(StringVar, ChrW(&H201C), "“")

Or, for symmetry, written in decimal (but I prefer hexadecimal for character codes):

StringVar = Replace(StringVar, ChrW(8220), "“")

Something else: the Replace function will probably soon be deprecated and doesn’t work everywhere (e.g. Windows Phone 7). Instead, use the Replace method of the String class:

StringVar = StringVar.Replace(, ChrW(8220), "“")
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 4
    OMG! Screw you, Visual Basic! – Mehrdad Afshari Jan 12 '11 at 08:30
  • 1
    Yeah, you're right. I didn't know that. But once you hear it, it makes perfect sense really. Great answer. +1 – Cody Gray - on strike Jan 12 '11 at 08:31
  • 1
    "““" doesn't work; the IDE replaces the curly quotes. ChrW saved my day, however. Thanks! – raven Jan 11 '12 at 09:38
  • I bet VB respects double curly quotes in string literals because MS expected some Word users to write macro code in Word itself, then realize their mistake and paste it into the macro editor, whereupon curly quotes would break the code. (pure conjecture) – easeout Feb 14 '12 at 19:29
  • 2
    @Kevin Nice conjecture but probably wrong: VB only started accepting curly quotes in .NET (VB7). At this time. VBA already existed but continued for years to use the VB6 dialect of the language which did *not* support curly quotes. So the feature was introduced into a language which wasn’t used for macros in Word. – Konrad Rudolph Feb 14 '12 at 21:29
  • @KonradRudolph The replace doesn't work. The following returns True. Dim stringVar As String stringVar = "““1““" Replace(stringVar, "““", "“") MessageBox.Show("""1""" = StringVar) – Zen Skunkworx Mar 27 '18 at 21:37
  • @ZenSkunkworx You’re using `Replace` wrongly. It doesn’t modify its argument, it returns the new string. That said, as noted in my answer this might still not work. Try using `ChrW` instead, that *will* work. And, as I said, this `Replace` function is deprecated. Use `String.Replace` instead. – Konrad Rudolph Mar 27 '18 at 22:01
0

See http://msdn.microsoft.com/en-us/library/613dxh46%28v=vs.71%29.aspx

Try this: StringVar = Replace(StringVar, "“", ChrW(&H8220))

0

It looks like you're searching for the ChrW function in the Microsoft.VisualBasic namespace, which is used to convert a Unicode character code into the actual character.

If you're trying to replace straight quotes in a string with curly quotes, try the following code:

'Declare a string that uses straight quotes
Dim origString As String = "This string uses ""quotes"" around a word."

'Create a new string by replacing the straight quotes from the original string
'with left-facing curly quotes
Dim newString As String = origString.Replace("""", ChrW(8220))

'Display the result
MessageBox.Show(newString)

Or, if you're trying to encode the left-facing curly quotes in a string by replacing them with an alternate notation (assuming the one you used in the question is correct), try the following code:

'Declare a string that uses left-facing curly quotes
Dim origString As String = "This string uses fancy " & ChrW(8220) & _
                           "quotes" & ChrW(8220) & " around a word."

'Create a new string by replacing the curly quotes with an arbitrary string
Dim newString As String = origString.Replace(ChrW(8220), "“")

'Display the result
MessageBox.Show(newString)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Cody, is it possible that you’re confusing a few things? The OP’s code looks like he wants to replace curly quotes with their HTML escape code. And that code is absolutely correct. Neither is “” wrong, nor is the semicolon. (I see that the OP has accepted your answer so perhaps I’m mistaken but I don’t think so). – Konrad Rudolph Jan 12 '11 at 08:06
  • @Konrad: Yeah, it's more than possible. I took a blind stab in the dark when trying to answer the question. It wasn't at all clear to me what (s)he is trying to accomplish. – Cody Gray - on strike Jan 12 '11 at 08:08
  • the code and the problem is actually pretty clear once you know one important factoid about VB, how it handles curly quotes, and escaping. ;-) The problem is, nobody, not even seasoned VB programmers, knows this …. See my answer. – Konrad Rudolph Jan 12 '11 at 08:18
  • I'm simply trying to compare 2 strings. The problem is that VB things that a string with normal double quotes is equivalent to the same string but with curly quotes. – Zen Skunkworx Mar 27 '18 at 21:40