c#替换"人物 [英] c# replace " characters

查看:31
本文介绍了c#替换"人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了一个 XML 字符串,我正在尝试通过 XmlReader 解析该字符串,我正在尝试删除 " 字符.

I am sent an XML string that I'm trying to parse via an XmlReader and I'm trying to strip out the " characters.

我试过了

.Replace(@"", "")
.Replace("\''", "''")
.Replace("\''", """)

加上其他几种方式.

有什么想法吗?

推荐答案

你是不是这样尝试的:

string text = GetTextFromSomewhere();
text.Replace("\", "");
text.Replace(""", "");

?如果是这样,那就是问题 - Replace 不会更改 original 字符串,它返回一个 new 字符串并执行替换...所以你会想要:

? If so, that's the problem - Replace doesn't change the original string, it returns a new string with the replacement performed... so you'd want:

string text = GetTextFromSomewhere();
text = text.Replace("\", "").Replace(""", "");

请注意,这将替换每个反斜杠每个双引号字符;如果您只想替换 pair 反斜杠后跟双引号",则只需使用:

Note that this will replace each backslash and each double-quote character; if you only wanted to replace the pair "backslash followed by double-quote" you'd just use:

string text = GetTextFromSomewhere();
text = text.Replace("\"", "");

(正如评论中提到的,这是因为字符串在 .NET 中是不可变的——一旦你以某种方式获得了一个字符串对象,该字符串将始终具有相同的内容.您可以将引用分配给一个不同的 字符串当然是一个变量,但这实际上并没有改变现有字符串的内容.)

(As mentioned in the comments, this is because strings are immutable in .NET - once you've got a string object somehow, that string will always have the same contents. You can assign a reference to a different string to a variable of course, but that's not actually changing the contents of the existing string.)

这篇关于c#替换"人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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