C# String.Replace 双引号和文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/285579/
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
C# String.Replace double quotes and Literals
提问by discorax
I'm fairly new to c# so that's why I'm asking this here.
我对 c# 相当陌生,所以这就是我在这里问这个的原因。
I am consuming a web service that returns a long string of XML values. Because this is a string all the attributes have escaped double quotes
我正在使用一个返回一长串 XML 值的 Web 服务。因为这是一个字符串,所有属性都转义了双引号
string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"
Here is my problem. I want to do a simple string.replace. If I was working in PHP I'd just run strip_slashes().
这是我的问题。我想做一个简单的string.replace。如果我在 PHP 中工作,我只会运行 strip_slashes()。
However, I'm in C# and I can't for the life of me figure it out. I can't write out my expression to replace the double quotes (") because it terminates the string. If I escape it then it has incorrect results. What am I doing wrong?
但是,我使用的是 C#,我终生无法弄清楚。我无法写出我的表达式来替换双引号 ("),因为它终止了字符串。如果我转义它,那么它会得到不正确的结果。我做错了什么?
string search = "\\"";
string replace = "\"";
Regex rgx = new Regex(search);
string strip = rgx.Replace(xmlSample, replace);
//Actual Result <root><item att1=value att2=value2 /></root>
//Desired Result <root><item att1="value" att2="value2" /></root>
MizardX: To include a quote in a raw string you need to double it.
MizardX:要在原始字符串中包含引号,您需要将其加倍。
That's important information, trying that approach now...No luck there either There is something going on here with the double quotes. The concepts you all are suggesting are solid, BUT the issue here is dealing with the double quotes and it looks like I'll need to do some additional research to solve this problem. If anyone comes up with something please post an answer.
这是重要的信息,现在尝试这种方法......也没有运气 这里双引号发生了一些事情。你们都提出的概念是可靠的,但这里的问题是处理双引号,看来我需要做一些额外的研究来解决这个问题。如果有人提出了一些问题,请发布答案。
string newC = xmlSample.Replace("\\"", "\"");
//Result <root><item att=\"value\" att2=\"value2\" /></root>
string newC = xmlSample.Replace("\"", "'");
//Result newC "<root><item att='value' att2='value2' /></root>"
采纳答案by ala
the following statement in C#
C#中的以下语句
string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"
will actually store the value
将实际存储值
<root><item att1="value" att2="value2" /></root>
whereas
然而
string xmlSample = @"<root><item att1=\""value\"" att2=\""value2\"" /></root>";
have the value of
具有以下价值
<root><item att1=\"value\" att2=\"value2\" /></root>
for the second case, you need to replace the slash () by empty string as follow
对于第二种情况,您需要将斜杠 () 替换为空字符串,如下所示
string test = xmlSample.Replace(@"\", string.Empty);
the result will be
结果将是
<root><item att1="value" att2="value2" /></root>
P.S.
聚苯乙烯
- slash (
\
) is default escape character in C# - to ignore slashes, use @ at the beginning of string
- if @ is used, the escape character is double quote (")
- 斜线 (
\
) 是 C# 中的默认转义字符 - 要忽略斜杠,请在字符串开头使用 @
- 如果使用@,则转义字符为双引号 (")
回答by Markus Jarderot
Both the string and the regex uses \
for escaping. The regex will see the character \
followed by "
, and think it's a literal escape. Try this:
字符串和正则表达式都\
用于转义。正则表达式会看到\
后跟的字符"
,并认为这是一个字面转义。尝试这个:
Regex rgx = new Regex("\\\"");
string strip = rgx.Replace(xmlSample, "\"");
You could also use raw strings (also known as veratim strings) in C#. They are prefixed with @
, and all back-slashes are treated as normal characters. To include a quote in a raw string you need to double it.
您还可以在 C# 中使用原始字符串(也称为 Veratim 字符串)。它们以 为前缀@
,所有反斜杠都被视为普通字符。要在原始字符串中包含引号,您需要将其加倍。
Regex rgx = new Regex(@"\""")
string strip = rgx.Replace(xmlSample, @"""");
Regex rgx = new Regex(@"\""")
string strip = rgx.Replace(xmlSample, @"""");
回答by Timothy Khouri
There's no reason to use a Regular expression at all... that's a lot heavier than what you need.
根本没有理由使用正则表达式......这比你需要的要重得多。
string xmlSample = "blah blah blah";
xmlSample = xmlSample.Replace("\\", "\"");
回答by balexandre
If you are getting an XML string why not use XML instead strings?
如果您得到一个 XML 字符串,为什么不使用 XML 代替字符串?
you will have access to all elements and attributes and it will be much easier and extremely fast if using the System.Xml namespace
您将可以访问所有元素和属性,如果使用 System.Xml 命名空间,它将变得更加容易和快速
in your example you are getting this string:
在你的例子中,你得到了这个字符串:
string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>";
All you need to do is convert that string into a XML Document and use it, like:
您需要做的就是将该字符串转换为 XML 文档并使用它,例如:
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.LoadXml(xmlSample);
System.Xml.XmlElement _root = xml.DocumentElement;
foreach (System.Xml.XmlNode _node in _root)
{
Literal1.Text = "<hr/>" + _node.Name + "<br/>";
for (int iAtt = 0; iAtt < _node.Attributes.Count; iAtt++)
Literal1.Text += _node.Attributes[iAtt].Name + " = " + _node.Attributes[iAtt].Value + "<br/>";
}
in ASP.NET this will output to the Literal1 something like:
在 ASP.NET 中,这将输出到 Literal1 类似于:
item
att1 = value
att2 = value2
once you have the element in a XmlElement, it is very easy to search and get the values and names for what's in that element.
一旦您在 XmlElement 中拥有元素,就很容易搜索并获取该元素中内容的值和名称。
give it a try, I use it a lot when retrieving WebServices responses and when I store something in a XML file as settings for a small application for example.
试一试,我经常在检索 WebServices 响应以及将某些内容存储在 XML 文件中作为小型应用程序的设置时使用它。