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

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

问题描述

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

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

看起来好像你想用等效的HTML代码替换卷曲。



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

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

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



回到你的卷曲根据VB语言规范(¶1.6.4),与直接引用相同,所以要在代码中写一个卷曲引用,请尝试以下操作:

  StringVar = Replace(StringVar,,“)

不幸的是,我现在不能尝试这个代码,这完全可以让IDE简单地用直接的引号替换,如果在这种情况下,另一种选择是使用左双引号的字符代码 Chr ChrW

  StringVar = Replace(StringVar,ChrW(& H201C),&#8220)
或者,为了对称,用十进制写(但是我更喜欢十六进制的字符代码):

  StringVar = Replace(StringVar,ChrW(8220),&#8220)

其他的东西:替换函数可能很快就会被弃用,不行(例如rel =noreferrer > Windows Phone 7 )。而是使用 String 类的替换方法:

  StringVar = StringVar.Replace(,ChrW(8220),&#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天全站免登陆