简体   繁体   English

查找并替换为查找文件

[英]Find and replace with lookup file

I want to find and replace text in a textfile with a lookup file. 我想查找文本文件并将其替换为查找文件。

Currently i use the following code to replace a single string: 目前,我使用以下代码替换单个字符串:

System.IO.StreamReader objReader;
objReader = new System.IO.StreamReader(someTextFile);
string content = objReader.ReadToEnd();
objReader.Close();

content = content.Replace("Text1", "Replacetext1");

System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(someTextFile);
objWriter.Write(content);
objWriter.Close();

I want to use a csv file for lookup. 我想使用一个csv文件进行查找。 The format of this file is: 该文件的格式为:

Text1, Replacedtext1 文字1,已取代文字1
Text2, Replacedtext2 文字2,替换文字2
Text3, Replacedtext3 文字3,替换文字3
etc... 等等...

Who can me give some tips? 谁可以给我一些提示?

Thanks 谢谢

OK Here are some tips: 好,这里有一些提示:

1) use 1)使用

string [] allLines = System.IO.File.ReadAllLines("yourLookUpFilePathHere");

it will return all lines in a string array. 它将返回字符串数组中的所有行。 The same you can do with your file in which you want to make the replacements. 您可以对要替换的文件执行相同的操作。 Now you can loop through the look up file. 现在您可以遍历查找文件。

2) At each position / line in the array you can use the method: 2)在数组的每个位置/每一行都可以使用以下方法:

string [] partsOfLine = allLines[i].Split('hereYourSeparatorAsChar');

3) now if you have fiddled out the first word you can start a second loop to look for matches in the replacement file and there you can use the line from your code: 3)现在,如果您整理了第一个单词,则可以启动第二个循环以在替换文件中查找匹配项,然后可以在代码中使用该行:

allLinesOfReplaceFile[i] = allLinesOfReplaceFile[i].Replace("Text1", "Replacetext1");

I hope you get the drift, if it is still unclear then drop me a comment 希望您能如愿以偿,如果仍然不清楚,请给我留言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM