c#怎么把整个txt文档中的某个字符后面的内容取出来,比如把所有A后面的三个字符取出来替换掉
1个回答
展开全部
FileStream fs = new FileStream(@"E:\test.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
String content = sr.ReadToEnd();
Char[] chars = content.ToArray();
for (int i = 0; i < content.Length; i++)
{
if (content[i] == 'A')
{
if (i + 3 < content.Length)
{
chars[i + 1] = '1';
chars[i + 2] = '2';
chars[i + 3] = '3';//此处替换成想要的字符
}
}
}
String newContent = new String(chars);
StreamReader sr = new StreamReader(fs);
String content = sr.ReadToEnd();
Char[] chars = content.ToArray();
for (int i = 0; i < content.Length; i++)
{
if (content[i] == 'A')
{
if (i + 3 < content.Length)
{
chars[i + 1] = '1';
chars[i + 2] = '2';
chars[i + 3] = '3';//此处替换成想要的字符
}
}
}
String newContent = new String(chars);
追问
怎么把修改之后的存到txt中呢?
追答
用StreamWriter。另外记得用完把流Close掉
sr.Close();
fs.Close();
StreamWriter sw = new StreamWriter(@"E:\test.txt", true);
sw.WriteLine(sw.NewLine + newContent);
sw.Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询