在以下代码中,字符串的使用"“"
(即字符串内的左双引号)会导致 VB.NET 中的编译错误:
StringVar = Replace(StringVar, "“", "“")
这里发生了什么?
在以下代码中,字符串的使用"“"
(即字符串内的左双引号)会导致 VB.NET 中的编译错误:
StringVar = Replace(StringVar, "“", "“")
这里发生了什么?
似乎您想用等效的 HTML 代码替换大引号。
乍一看,您的代码绝对正确。问题是 VB 允许在代码中用大引号代替常规引号(因为 Unicode 很棒,对吧?)。也就是说,下面的代码都是等价的:
Dim str = "hello"
Dim str = “hello”
Dim str = "hello“
现在,如果你想在字符串中使用引号,VB 不知道引号是否应该结束字符串。在 C# 中,这将通过转义引号来解决,即代替"""
你写的"\""
. 在 VB 中,同样的方法是双引号,即""""
.
回到你的卷曲报价。根据 VB 语言规范 (¶1.6.4),与直引号相同。因此,要在代码中编写花引号,请尝试以下操作:
StringVar = Replace(StringVar, "““", "“")
不幸的是,我现在无法尝试此代码,并且 IDE 完全有可能简单地将其替换为直引号。如果是这种情况,另一种方法是使用Chr
或ChrW
与“左双引号”的字符代码一起使用:
StringVar = Replace(StringVar, ChrW(&H201C), "“")
或者,为了对称,用十进制写(但我更喜欢十六进制的字符代码):
StringVar = Replace(StringVar, ChrW(8220), "“")
别的东西:该Replace
功能可能很快就会被弃用,并且不能在任何地方工作(例如Windows Phone 7)。相反,使用类的Replace
方法String
:
StringVar = StringVar.Replace(, ChrW(8220), "“")
请参阅http://msdn.microsoft.com/en-us/library/613dxh46%28v=vs.71%29.aspx
试试这个:
StringVar = Replace(StringVar, "“", ChrW(&H8220))
看起来您正在命名空间中搜索ChrW
函数 ,该函数Microsoft.VisualBasic
用于将 Unicode 字符代码转换为实际字符。
如果您尝试用花引号替换字符串中的直引号,请尝试以下代码:
'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)
或者,如果您尝试通过用另一种表示法替换它们来对字符串中的左花引号进行编码(假设您在问题中使用的符号是正确的),请尝试以下代码:
'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)