本文介绍了如何使用C#替换文本文件中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



请帮我解决这个问题。



我有一个文本文件这有很多行,(某些行有不正确的单词),我需要用正确的单词替换不正确的单词。



如果我按照这个

Hello All,

Kindly help me out with this.

I have a text file and this has many lines,(some line have incorrect words), and I need to replace the incorrect words by correct words.

If I follow this

String strFile = File.ReadAllText("c:\\File1.txt");

strFile = strFile.Replace("oldvalue", "newvalue");

File.WriteAllText("c:\\File1.txt", strFile);





(我实际上不想跟随)我有其他问题,一些正确的单词被改变。

还有其他方法可以做到这一点。



(Which I actually dont want to follow) I have other problems line some correct words getting changed.
Is there any other way to do this.

推荐答案

string text = File.ReadAllText(@"c:\File1.txt");
text = text.Replace("old value","new stuff");
File.WriteAllText(@"c:\File1.txt", text);





看!没有正则表达式!



Look! No Regex!


string strFile = File.ReadAllText(@"c:\File1.txt");
strFile = Regex.Replace(strFile, @"\boldvalue\b", "newvalue");
File.WriteAllText(@"c:\File1.txt", strFile);



这篇关于如何使用C#替换文本文件中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 15:32