Click here to Skip to main content
15,882,376 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
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.
Updated 15-Dec-22 22:20pm
Dalek Dave 21-Jul-10 19:02pm    
Edited for Grammar, Syntax and Capitalisation.

A variation of Toli's code:

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

Look! No Regex!
Share this answer
Toli Cuturicu 26-Jul-10 13:51pm    
Reason for my vote of 1
This will not work. Parts of words may get replaced.
(text = "wordpad and winword are word processors,
text.Replace("word", "good word")
Marcus Riggs 13-Jul-18 22:51pm    
#realJSOP 14-Jul-18 14:08pm    
That’s impossible to do.
Yes. Use REGular EXpressions!
Take a look at the Regex class.

For example something like this:
string strFile = File.ReadAllText(@"c:\File1.txt");
strFile = Regex.Replace(strFile, @"\boldvalue\b", "newvalue");
File.WriteAllText(@"c:\File1.txt", strFile);
Share this answer
asjadazeez 21-Jul-10 3:39am    
OK this does not work. reason i actually want that data to be writen on the file . My need is that i open a text file. search for a string, and this search i do using filestream and i search the string in each line, if the string is found in the line the, i need to replace that string with a new string. Regex give me methods to replace string but not in the file. Is there any way i can replace the string in the file.
Toli Cuturicu 21-Jul-10 18:10pm    
I edited the answer a little. Look again, please.
#realJSOP 21-Jul-10 18:18pm    
Why bother with Regex when the string object has a Replace method. You're using a sledge hammer to kill a fly.
Christian Graus 21-Jul-10 18:53pm    
There is no way to change the text IN the file. You need to read the file in to memory, change it, and save it over the old file.
Sandeep Mewara 22-Jul-10 10:09am    
You gave a comment to me here ""
Did you read the original question, the one i replied to before commenting?
Why don't you want to follow the only reasonable way to do this ? How is the computer supposed to know which values of 'oldvalue' are 'right' ? You'll need to write your own code to work that out, in which case you need to parse all the text, and make the changes in place, one at a time. There is no way to 'magically' change only the instances you want to, unless you can use regex to determine a rule that always applies to find just the ones you want to change.
Share this answer
asjadazeez 21-Jul-10 23:26pm    
ok so finally the best way to edit text in a file is to read it line by line and over write it again on the same file. This should be OK.Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900