[英]Replace multiple strings in text files with different texts
I have a text file like so:我有一个像这样的文本文件:
template.txt模板.txt
hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat [FOOD]
and I am trying to replace whatever is in the square brackets with strings from a list example我正在尝试用列表示例中的字符串替换方括号中的任何内容
// // name //country // age // food
p.Add(new Person("jack", "NZ", "20", "Prawns"));
p.Add(new Person("ana", "AUS", "23", "Chicken"));
p.Add(new Person("tom", "USA", "30", "Lamb"));
p.Add(new Person("ken", "JAPAN", "15", "Candy"));
so far I have tried the below function which I call inside a loop到目前为止,我已经尝试了以下 function 我在循环中调用
//loop
static void Main(string[] args)
{
int count = 0;
foreach (var l in p)
{
FindAndReplace("template.txt","output"+count+".txt" ,"[MYNAME]",l.name);
FindAndReplace("template.txt","output"+count+".txt" ,"[COUNTRY]",l.country);
FindAndReplace("template.txt","output"+count+".txt" ,"[AGE]",l.age);
FindAndReplace("template.txt","output"+count+".txt" ,"[FOOD]",l.food);
count++;
}
}
//find and replace function
private static void FindAndReplace(string template_path,string save_path,string find,string replace)
{
using (var sourceFile = File.OpenText(template_path))
{
// Open a stream for the temporary file
using (var tempFileStream = new StreamWriter(save_path))
{
string line;
// read lines while the file has them
while ((line = sourceFile.ReadLine()) != null)
{
// Do the word replacement
line = line.Replace(find, replace);
// Write the modified line to the new file
tempFileStream.WriteLine(line);
}
}
}
}
this is what I have done.这就是我所做的。 But the output I get is this但是我得到的 output 是这个
output1.txt输出1.txt
hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat Prawns
output2.txt输出2.txt
hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat Chicken
Only the last text is replaced.只有最后一个文本被替换。
Every time you call FindAndReplace
you are overwriting the last file written.每次调用FindAndReplace
时,都会覆盖最后写入的文件。
When you call it the first time it reads the template file, replaces a specific placeholder ( [MYNAME]
) with a value and writes it to a new file.当您第一次调用它时,它会读取模板文件,将特定占位符 ( [MYNAME]
) 替换为一个值并将其写入新文件。 In the next call you take the template again so [MYNAME]
is not replaced anymore and only replaces the country and writes it to the same file overwriting the content.在下一次调用中,您再次使用模板,因此[MYNAME]
不再被替换,只会替换国家并将其写入同一个文件以覆盖内容。 This repeats till you get to the last call.这会一直重复,直到您打到最后一个电话。
That is why only [FOOD]
is replaced.这就是为什么只有[FOOD]
被替换的原因。 Try replacing all the text in one go and then writing it to the file.尝试替换 go 中的所有文本,然后将其写入文件。
instead of a function try doing something like this而不是 function 尝试做这样的事情
static void Main(string[] args)
{
int count = 0;
foreach (var l in p)
{
using (var sourceFile = File.OpenText("template.txt"))
{
// Open a stream for the temporary file
using (var tempFileStream = new StreamWriter("output" + count + ".txt"))
{
string line;
// read lines while the file has them
while ((line = sourceFile.ReadLine()) != null)
{
line = line.Replace("[MYNAME]", l.name);
line = line.Replace("[COUNTRY]", l.country);
line = line.Replace("[AGE]", l.age);
line = line.Replace("[FOOD]", l.food);
tempFileStream.WriteLine(line);
}// end of while loop
}
count++;
}//end foreach loop
}
}//end of main
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.