C#字符串替换,不工作 [英] C# string replacement , not working

查看:107
本文介绍了C#字符串替换,不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我从阅读的字符串:

I have a string which I read in from :

TextReader tr = new StreamReader(this.dataPath );
string contents = tr.ReadToEnd();

含量值开头:

"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n....."

当我尝试执行

        string styleSheet = "<?xml-stylesheet type=\"text/xsl\" href=\"message.xsl\"?>";
        string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        TextReader tr = new StreamReader(this.dataPath );
        string contents = tr.ReadToEnd(); 
        contents.Replace(xmlString,xmlString + styleSheet );

这绝对不会找到了XmlString中第一次出现。任何想法,为什么?

It absolutely won't find the first occurance of XmlString. Any ideas why?

推荐答案

尝试

contents = contents.Replace(xmlString,xmlString + styleSheet );

这是因为String类是不可变的。

This is because the String class is immutable.

这篇关于C#字符串替换,不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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