为什么尝试逐字逐字化此字符串会失败?
Why does attempting to verbatimize this string fail?
我有这个子字符串,我想从字符串中删除:
<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">
意识到它充满了乐趣,我认为逐字记录它可以解决所有问题:
String messedUpJunk = @"<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">";
...但是,用《迷失太空》中的机器人来解释,它不计算(编译);我在第一个 "http".
得到“;预计”
我可以通过转义引号使其可编译:
String messedUpJunk = "<ArrayOfSiteQuery xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS\">";
...但如果不是逐字逐句的,那有什么用呢?
双引号是您在逐字字符串中唯一需要转义的字符。转义它的方式不同,您需要将其加倍 (""
) 而不是使用反斜杠:
String messedUpJunk = @"<ArrayOfSiteQuery xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS"">";
Use double quotation marks to embed a quotation mark inside a verbatim string
是需要转义的双引号(使用双引号),其余字符不需要转义,例如反斜杠\
.
参见:2.4.4.5 String literals - C#
A verbatim string literal consists of an @ character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is @"hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences and hexadecimal and Unicode escape
sequences are not processed in verbatim string literals. A verbatim
string literal may span multiple lines.
你需要转义双引号的原因是因为它代表字符串的开始和结束,无论是逐字的还是常规的。
我有这个子字符串,我想从字符串中删除:
<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">
意识到它充满了乐趣,我认为逐字记录它可以解决所有问题:
String messedUpJunk = @"<ArrayOfSiteQuery xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS">";
...但是,用《迷失太空》中的机器人来解释,它不计算(编译);我在第一个 "http".
得到“;预计”我可以通过转义引号使其可编译:
String messedUpJunk = "<ArrayOfSiteQuery xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS\">";
...但如果不是逐字逐句的,那有什么用呢?
双引号是您在逐字字符串中唯一需要转义的字符。转义它的方式不同,您需要将其加倍 (""
) 而不是使用反斜杠:
String messedUpJunk = @"<ArrayOfSiteQuery xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS"">";
Use double quotation marks to embed a quotation mark inside a verbatim string
是需要转义的双引号(使用双引号),其余字符不需要转义,例如反斜杠\
.
参见:2.4.4.5 String literals - C#
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.
你需要转义双引号的原因是因为它代表字符串的开始和结束,无论是逐字的还是常规的。