在VB中的字符串中使用左双引号 [英] Using left double quotation marks in strings in VB

查看:622
本文介绍了在VB中的字符串中使用左双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,字符串(即字符串中的左双引号)的使用导致VB.NET中的编译错误: / p>

  StringVar = Replace(StringVar,,“)
解决方案



第一眼看来,你的代码是绝对正确的。问题是VB允许卷曲引号代替代码中的常规引号(因为Unicode是伟大的,对吗?)。也就是说,以下代码全部等效:

  Dim str =hello
Dim str =hello
Dim str =hello

现在,如果要使用引号里面一个字符串,VB不知道引号是否应该结束字符串在C#中,这将通过转义引号的地方,你会写\。在VB中, 引号,即



返回您的卷曲报价因此要在代码中写一个curly引用,请尝试以下:

  StringVar = Replace(StringVar,,“)


b $ b

不幸的是,我现在不能尝试这个代码,而且IDE完全可以用直接的引号替换它。如果是这样,一个替代方法是使用 Chr ChrW ,其中带有左双引号的字符代码:

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


$ b b

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

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

c $ c>替换函数可能很快就会被弃用,并且无法在任何地方工作(例如 Windows Phone 7 )。而是使用 String 类的替换方法:

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


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?

解决方案

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

这篇关于在VB中的字符串中使用左双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆