1 /// <summary> 2 /// 替换文本文件中的词 3 /// </summary> 4 /// <param name="filePath"></param> 5 /// <param name="oldWord"></param> 6 /// <param name="newWord"></param> 7 public static void ReplaceWordInTxt(string fliePath, string oldWord, string newWord) 8 { 9 string target = File.ReadAllText(fliePath); 10 var strFile = Regex.Replace(target, @"\b" + oldWord + @"\b", newWord); 11 File.WriteAllText(fliePath, strFile); 12 }
参考:https://stackoverflow.com/questions/13544425/how-can-i-replace-a-word-in-a-txt-file-in-c-sharp
标签:Regex,string,文本文件,oldWord,fliePath,txt From: https://www.cnblogs.com/frankwu2014/p/16905814.html