Click here to Skip to main content
14,667,947 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted
Updated 23-Apr-18 4:25am
v2
Comments
Dalek Dave 21-Jul-10 19:02pm
   
Edited for Grammar, Syntax and Capitalisation.

Rate this:
Please Sign up or sign in to vote.

Solution 2

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!
   
Comments
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
   
WILL THIS REPLACE TEXT IN A TEXT FILE WITHOUT OPENING IT :)
#realJSOP 14-Jul-18 14:08pm
   
That’s impossible to do.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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);
   
v3
Comments
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 "http://www.codeproject.com/Answers/95939/Windows-Service.aspx#answer1"
Did you read the original question, the one i replied to before commenting?
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
   
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100