Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to increase the speed of my program by find & replace text in bulk using c#.net, i am currently doing find & replace by finding & replacing each word one by one . i want to replace
it in bulk, so that speed & performance will increase. how to do it?, Any help would be great.
Posted

1 solution

if you want replace all word at a time

step1: store all word in a list

List<string> wordsToReplace = new List<string>() { "age:-21","salary:-20000","Name:-vikas" };

step2: create an expression

string replacementString = String.Join("|",wordsToReplace);
step3: store document

string fileContent = System.IO.File.ReadAllText(Server.MapPath("~/TestFile.doc"));

step4: Perform replacement
fileContent = Regex.Replace(fileContent,replacementString, "animal")

step5:write contenet

System.IO.File.WriteAllText(Server.MapPath("~/TestFile.doc"), fileContent);
 
Share this answer
 
Comments
Member 10843516 5-Sep-14 1:05am    
I am having windows application so i guess Server. Map path along with Regular expression (Regex) will not be applicable. Is there any other alternative?
vangapally Naveen Kumar 5-Sep-14 1:57am    
Mention your file path instead of server.mappath and regular expression work in windows app also..

see this link

http://stackoverflow.com/questions/5651464/c-sharp-word-replace-with-formating
Member 10843516 9-Sep-14 4:03am    
I have tried this code in windows application but that word file is getting corrupted & that word file is not getting open.
I have write filepath instead of server.mappath & i am keep getting the same error.
Any help would be great.

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