簡體   English   中英

用不同的文本替換文本文件中的多個字符串

[英]Replace multiple strings in text files with different texts

我有一個像這樣的文本文件:

模板.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat [FOOD]

我正在嘗試用列表示例中的字符串替換方括號中的任何內容

//         // 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"));

到目前為止,我已經嘗試了以下 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);
                    }
                }
            }
  
        }

這就是我所做的。 但是我得到的 output 是這個

輸出1.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat Prawns

輸出2.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat Chicken

只有最后一個文本被替換。

每次調用FindAndReplace時,都會覆蓋最后寫入的文件。

當您第一次調用它時,它會讀取模板文件,將特定占位符 ( [MYNAME] ) 替換為一個值並將其寫入新文件。 在下一次調用中,您再次使用模板,因此[MYNAME]不再被替換,只會替換國家並將其寫入同一個文件以覆蓋內容。 這會一直重復,直到您打到最后一個電話。

這就是為什么只有[FOOD]被替換的原因。 嘗試替換 go 中的所有文本,然后將其寫入文件。

而不是 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM