我从一个文本文件中读取数据,得到一个string ,然后想将某些字符串替换掉,但是不能成功,请指教:
文本文件:
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = iappProjName, iappProjNamePath, iappProjGuid
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal读取的程序如下: string slnMoban = @"..\..\..\ucodeWinForm\slnMoban.txt";
StreamReader sr = new StreamReader(slnMoban, System.Text.Encoding.UTF8);
string readSlnFile = sr.ReadToEnd();
sr.Close(); readSlnFile.Replace("iappProjName", "rrrrrrrrrrr");
Console.WriteLine(readSlnFile);
Console.ReadLine();最后看输出,iappProjName无法被替换。请达人帮忙。
文本文件:
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = iappProjName, iappProjNamePath, iappProjGuid
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25A0E2BB-2AF8-4057-A875-C588DC1C2729}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal读取的程序如下: string slnMoban = @"..\..\..\ucodeWinForm\slnMoban.txt";
StreamReader sr = new StreamReader(slnMoban, System.Text.Encoding.UTF8);
string readSlnFile = sr.ReadToEnd();
sr.Close(); readSlnFile.Replace("iappProjName", "rrrrrrrrrrr");
Console.WriteLine(readSlnFile);
Console.ReadLine();最后看输出,iappProjName无法被替换。请达人帮忙。
Console.WriteLine(readSlnFile);
返回的对象才是你要的.
因为STRING是不可变的.
//改为
readSlnFile = readSlnFile.Replace("iappProjName", "rrrrrrrrrrr");
内部有string常量池来维护的.不建义大量处理string.
StreamReader sr = new StreamReader(slnMoban, System.Text.Encoding.UTF8);
string readSlnFile = sr.ReadToEnd().Replace("iappProjName", "rrrrrrrrrrr");
sr.Close(); Console.WriteLine(readSlnFile);
Console.ReadLine();
readSlnFile.Replace("iappProjName", "rrrrrrrrrrr");
//改为
readSlnFile = readSlnFile.Replace("iappProjName", "rrrrrrrrrrr");