在VB中的字符串中使用左双引号
在下面的代码中,使用字符串 "“"
(即字符串内的左双引号)会导致 VB.NET 中出现编译错误:
StringVar = Replace(StringVar, "“", "“")
这是怎么回事?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在下面的代码中,使用字符串 "“"
(即字符串内的左双引号)会导致 VB.NET 中出现编译错误:
StringVar = Replace(StringVar, "“", "“")
这是怎么回事?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
似乎您想用等效的 HTML 代码替换大引号。
乍一看,您的代码绝对正确。问题是 VB 允许在代码中用大引号代替常规引号(因为 Unicode 很棒,对吧?)。也就是说,以下代码都是等效的:
现在,如果您想在字符串内使用引号,VB 不知道引号是否应该结束字符串。在 C# 中,可以通过转义引号来解决这个问题,即用
"\""
代替"""
。在 VB 中,通过双引号完成相同的操作,即""""
。回到你的弯引号。根据 VB 语言规范 (¶1.6.4),与直引号相同。因此,要在代码中编写弯引号,请尝试以下操作:
不幸的是,我现在无法尝试此代码,IDE 完全有可能只是将其替换为直引号。如果是这种情况,另一种方法是使用
Chr
或ChrW
以及“左双引号”的字符代码:或者,为了对称,写为十进制(但我更喜欢十六进制字符代码):
其他:
Replace
函数可能很快就会被弃用,并且 并非在任何地方都有效(例如 Windows Phone 7)。相反,请使用String
类的Replace
方法: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:
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:
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
orChrW
with the character code of the “left double quotation mark”:Or, for symmetry, written in decimal (but I prefer hexadecimal for character codes):
Something else: the
Replace
function will probably soon be deprecated and doesn’t work everywhere (e.g. Windows Phone 7). Instead, use theReplace
method of theString
class:请参阅 http://msdn.microsoft.com/ en-us/library/613dxh46%28v=vs.71%29.aspx
试试这个:
StringVar = Replace(StringVar, "“", ChrW(&H8220))
See http://msdn.microsoft.com/en-us/library/613dxh46%28v=vs.71%29.aspx
Try this:
StringVar = Replace(StringVar, "“", ChrW(&H8220))
看起来您正在搜索
ChrW
函数,用于将 Unicode 字符代码转换为实际字符。< /strong>如果您尝试用弯引号替换字符串中的直引号,请尝试以下代码:
或者,如果您尝试将字符串中向左的弯引号替换为替代符号 (假设您在问题中使用的代码是正确的),请尝试以下代码:
It looks like you're searching for the
ChrW
function in theMicrosoft.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:
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: