在VB中的字符串中使用左双引号

发布于 2024-10-11 06:13:22 字数 161 浏览 10 评论 0 原文

在下面的代码中,使用字符串 "“"(即字符串内的左双引号)会导致 VB.NET 中出现编译错误:

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

这是怎么回事?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨夜星沙 2024-10-18 06:13:22

似乎您想用等效的 HTML 代码替换大引号。

乍一看,您的代码绝对正确。问题是 VB 允许在代码中用大引号代替常规引号(因为 Unicode 很棒,对吧?)。也就是说,以下代码都是等效的:

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

现在,如果您想在字符串内使用引号,VB 不知道引号是否应该结束字符串。在 C# 中,可以通过转义引号来解决这个问题,即用 "\"" 代替 """。在 VB 中,通过双引号完成相同的操作,即 """"

回到你的弯引号。根据 VB 语言规范 (¶1.6.4),与直引号相同。因此,要在代码中编写弯引号,请尝试以下操作:

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

不幸的是,我现在无法尝试此代码,IDE 完全有可能只是将其替换为直引号。如果是这种情况,另一种方法是使用 ChrChrW 以及“左双引号”的字符代码:

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

或者,为了对称,写为十进制(但我更喜欢十六进制字符代码):

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

其他:Replace 函数可能很快就会被弃用,并且 并非在任何地方都有效(例如 Windows Phone 7)。相反,请使用 String 类的 Replace 方法:

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

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), "“")
第七度阳光i 2024-10-18 06:13:22

请参阅 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))

追星践月 2024-10-18 06:13:22

看起来您正在搜索ChrW 函数,用于将 Unicode 字符代码转换为实际字符。< /strong>

如果您尝试用弯引号替换字符串中的直引号,请尝试以下代码:

'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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文