C#与string.replace双引号和文字 [英] C# String.Replace double quotes and Literals

查看:261
本文介绍了C#与string.replace双引号和文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的C#所以这就是为什么我问这在这里。



我耗费返回XML值的长字符串的Web服务。因为这是一个字符串的所有属性都逃脱双引号

 字符串xmlSample =<根><项目ATT1 = \\ \\value\ATT2 = \value2\/>< /根>中

下面是我的问题。我想做一个简单的与string.replace。如果我是PHP的工作我只是跑strip_slashes()。



不过,我在C#中,我不能为我的生命数字出来。我不能写出我的表情来代替双引号(),因为它终止字符串。如果我逃避它,它已不正确的结果。我在做什么错了?



 字符串搜索=\\\; 
字符串替换=\;
正则表达式RGX =新的正则表达式(搜索);
串条= rgx.Replace(xmlSample,替换);

//实际结果<根><项目ATT1 =值ATT2 =值2 />< /根>
//所需的结果<根><项目ATT1 =值ATT2 =VALUE2 />< /根>




MizardX:要包含报价在原始字符串,你需要加倍。




这是很重要的信息,现在想这种做法......没有运气或者
也有一些是用双引号怎么回事。你们所提出的建议的概念是固体,但这里的问题是处理双引号,它看起来像我需要做一些额外的研究来解决这个问题。如果有人想出了东西请邮寄的答案。

 字符串NEWC = xmlSample.Replace(\\\ ,\); 
//结果<根和GT;<项目ATT = \value\ATT2 = \value2\/>< /根>

串NEWC = xmlSample.Replace(\,');
//结果NEWC<根><项目ATT ='值'ATT2 ='值2/>< /根>中


解决方案

在C#下面的语句

 字符串xmlSample =<根><项目ATT1 = \value\ATT2 = \value2\/>< /根>中

将实际存储值

 <根><项目ATT1 =值ATT2 =值2/>< /根> 

,而

 字符串xmlSample = @<根><项目ATT1 = \value\ATT2 = \value2\/>< /根>中,

 <值code><根><项目ATT1 = \value\ATT2 = \value2\/>< /根> 

对于第二种情况,则需要通过空字符串作为后续



<$ p来代替斜杠() $ p> 字符串测试= xmlSample.Replace(@\的String.Empty);



结果将是

 <根和GT;<项目ATT1 =值ATT2 =值2/>< /根> 



P.S。




  1. 斜杠( \ )是在C#
  2. 缺省转义字符
  3. 要在字符串的开头

  4. 如果@用于忽略斜杠,使用@转义字符是双引号()


I'm fairly new to c# so that's why I'm asking this here.

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

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().

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?

    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.

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>"

解决方案

the following statement in 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.

  1. slash (\) is default escape character in C#
  2. to ignore slashes, use @ at the beginning of string
  3. if @ is used, the escape character is double quote (")

这篇关于C#与string.replace双引号和文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆