简体   繁体   English

逃离C#,有点困惑

[英]Escaping C#, little baffled

I feel stupid for asking such a question. 问这样一个问题我觉得很蠢。 How do I escape double quotes? 如何逃避双引号?

I've tried Google and it says to use \\" but it don't seem to work. 我已经尝试了谷歌并且它说使用“​​但它似乎不起作用。

string html = @"<a href=\"http://google.com\">Test</a>";

It returns 它回来了

';' expected

What am I doing wrong? 我究竟做错了什么?

One of these: 其中之一:

string html = "<a href=\"http://google.com\">Test</a>";
string html = @"<a href=""http://google.com"">Test</a>";

The escaping varies between the two forms. 逃逸在两种形式之间变化。 The second, with the "@" prefix, is known as a verbatim string literal , allows line breaks and so on, and doesn't treat the backslash as a special escaping character. 第二个带有“@”前缀,称为逐字字符串文字 ,允许换行符等,并且不将反斜杠视为特殊的转义字符。 It thus handles everything except the double quote without need for escaping - and the double quote is escaped by doubling it so it isn't interpreted as the end of the string literal. 因此它处理除双引号之外的所有内容而不需要转义 - 并且双引号通过加倍来转义,因此它不会被解释为字符串文字的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM