我想逐行读取文本文件并编辑特定行。因此,我已将文本文件放入一个字符串变量中,例如:
string textFile = File.ReadAllText(filename);
我的文本文件是这样的:
Line A
Line B
Line C
Line abc
Line 1
Line 2
Line 3
我有一个特定的字符串 (="abc"),我想在这个 textFile 中搜索它。所以,我正在阅读这些行,直到找到字符串并在找到的字符串之后转到第三行(“第 3 行”-> 这行总是不同的):
string line = "";
string stringToSearch = "abc";
using (StringReader reader = new StringReader(textFile))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(stringToSearch))
{
line = reader.ReadLine();
line = reader.ReadLine();
line = reader.ReadLine();
//line should be cleared and put another string to this line.
}
}
}
我想清除第三个读取行并将另一个字符串放入该行并将整个保存string
到textFile
.
我怎样才能做到这一点?