C# 写一个包含“”的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/974877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a string which contains " "
提问by GurdeepS
I have a string from an xml document:
我有一个来自 xml 文档的字符串:
<title type="html">
Is there a way for me to write this string like "<title type="html">"
? I've had similar problems writing javascript as a string but I did see some solutions in the past (which I can't remember)?
有没有办法让我像这样写这个字符串"<title type="html">"
?我在将 javascript 编写为字符串时遇到了类似的问题,但我过去确实看到了一些解决方案(我不记得了)?
Thanks
谢谢
采纳答案by Simon P Stevens
You can escape quotes in a string using a \
您可以使用 \ 转义字符串中的引号
String s = "this is my \"data\" in the string";
回答by gubby
Escape:
逃脱:
var str = "<font color=\"red\">;";
(Edit: forgot to put proper html chars in!)
(编辑:忘记输入正确的 html 字符了!)
Or in javascript you can use single quotes to contain one with doubles:
或者在 javascript 中,您可以使用单引号来包含双引号:
var str = '<font color="red">';
回答by Eoin Campbell
You need to escape your double quotes..
你需要转义你的双引号..
string blah = "<title type=\"html\">";
OR
string blah = @"<title type=""html"">";
Alternatively you could use single quotes in your tag, which will serve the same purpose.
或者,您可以在标签中使用单引号,这将起到相同的作用。
string blah = "<title type='html'>";
回答by Daren Thomas
You can also write it like this:
你也可以这样写:
string f = @"<font color=""red"">";
Check out msdn on string literals.
查看msdn 上的字符串文字。
回答by SLaks
If you're making a large string with a lot of XML, one approach would be to write
如果您使用大量 XML 制作一个大字符串,一种方法是编写
var str = @"<tag attribute=`value`><SubTag OtherAttribute=`OtherValue` /></tag>".Replace('`', '"');
Beware that this will be less efficient at runtime, because of the call to Replace().
请注意,由于调用 Replace(),这在运行时会降低效率。
The best way to handle this is might be to put the markup in a separate text file and embed it in a ResX file. That would allow you to write Properties.Resources.Markup
.
处理此问题的最佳方法可能是将标记放在单独的文本文件中并将其嵌入 ResX 文件中。这将允许您编写Properties.Resources.Markup
.
回答by jay_t55
string jason = "This string contains a \"";
string jason = "这个字符串包含一个\"";