gpt4 book ai didi

vb.net - VB 不考虑 "and ” 1” 之间的区别

转载 作者:行者123 更新时间:2023-12-02 08:08:15 25 4
gpt4 key购买 nike

我们有数据,其中的键是可以包含引号的字符串。问题是 Visual Basic 不区分普通双引号和斜引号。例如。声明:

MessageBox.Show("""1""" = "””1””") 

返回真。请注意,我们正在从 python 程序生成非常大的脚本文件,并在 VB 脚本环境中运行它们。我们不是从文件中读取数据。我怎样才能让 VB 尊重这两种引号之间的区别?

最佳答案

您正在反对 VB.Net 语言规范,该规范在代码语句中将三个不同的双引号字符视为相同的字符。

来自 String Literals :

A string literal is a sequence of zero or more Unicode characters beginning and ending with an ASCII double-quote character, a Unicode left double-quote character, or a Unicode right double-quote character. Within a string, a sequence of two double-quote characters is an escape sequence representing a double quote in the string.

StringLiteral
: DoubleQuoteCharacter StringCharacter* DoubleQuoteCharacter
;

DoubleQuoteCharacter
: '"'
| '<unicode left double-quote 0x201c>'
| '<unicode right double-quote 0x201D>'
;

StringCharacter
: '<Any character except DoubleQuoteCharacter>'
| DoubleQuoteCharacter DoubleQuoteCharacter
;

在上面引用的规范中,“ASCII 双引号字符”的使用是指英寸字符 或Chrw(34)。

在 VS2015 之前,您甚至无法将 """1"""= "””1””" 粘贴到代码编辑器中,而不会自动将其转换为 """1 """= """1"""

如果您需要构造包含 Unicode 双引号的代码语句,则需要使用它们各自的字符表示来构造它们。

Const ucDoubleLeftQuote As Char = ChrW(&H201C) ' "“"c
Const ucDoubleRightQuote As Char = ChrW(&H201D) ' "”"c
Const asciiDoubleQuote As Char = ChrW(34) ' """"c

Dim asciiQuoted As String = """1"""
Dim asciiQuotedv2 As String = asciiDoubleQuote & "1" & asciiDoubleQuote

Dim unicodeQuoted As String = ucDoubleLeftQuote & "1" & ucDoubleLeftQuote

MessageBox.Show((asciiQuoted = asciiQuotedv2).ToString()) ' yields true
MessageBox.Show((asciiQuoted = unicodeQuoted).ToString()) ' yields false

编辑:为了演示 VB 编译器用 ASCII 双引号替换字符串文字中的任何 Unicode 双引号,请考虑以下代码。

Module Module1
Sub Main()
T1("““ 1 ””") ' unicode quotation marks left and right
T2(""" 1 """) ' ascii quotation mark
End Sub
Sub T1(s As String) ' dummy method to highlight unicode quotation mark
End Sub
Sub T2(s As String) ' dummy method to highlight asci quotation mark
End Sub
End Module

当在 ILDASM 中查看时,此代码将产生以下 IL。

.method public static void  Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 24 (0x18)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "\" 1 \""
IL_0006: call void ConsoleApp1.Module1::T1(string)
IL_000b: nop
IL_000c: ldstr "\" 1 \""
IL_0011: call void ConsoleApp1.Module1::T2(string)
IL_0016: nop
IL_0017: ret
} // end of method Module1::Main

IL_0001: ldstr "\"1\"" 对应调用语句的字符串加载:T1("““1 ””")

你可以看到,这与 IL_000c: ldstr "\"1\"" 相同,对应于调用语句的字符串加载:T2("""1 """).

关于vb.net - VB 不考虑 "and ” 1” 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49522461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com