Using left double quotation marks in strings in VB

ⅰ亾dé卋堺 提交于 2019-11-28 07:02:30

问题


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?


回答1:


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), "“")



回答2:


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

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




回答3:


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)


来源:https://stackoverflow.com/questions/4665510/using-left-double-quotation-marks-in-strings-in-vb

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