小编nag*_*508的帖子

将文本文件中的多个字符串替换为不同的文本

我有一个像这样的文本文件:

模板.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat [FOOD]
Run Code Online (Sandbox Code Playgroud)

我试图用列表示例中的字符串替换方括号中的内容

//         // 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"));
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试了以下在循环内调用的函数

//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 …
Run Code Online (Sandbox Code Playgroud)

c# streamreader

1
推荐指数
1
解决办法
897
查看次数

标签 统计

c# ×1

streamreader ×1