Hi guys,
I need to design a parser in c# which parses a text file and replaces a given text with another text, moreover it should comment the previous text,eg:
Before:
this is sam
After:
' this is sam // original statement
this is not sam // replaced
Can someone pls guide me through this.
thanks
Sid
Siddharth TelkarPosted Aug 27, 2008, 2:37 AM
hi Alan ,
Firsts of all thanks for that code, it really worked as i wanted it to. But it replaces a single original string with the replacement string. can u pls tell me how to replace multiple original striong with their equivalents in the file. for this i am using a xml file in which a node will contain a "original string" and the other node will contain "replacement string", the string will be distinguished by the ID in the XML. this structure of xml file is like this....
:
:
Can u pls tell me how to go abt this.
thanks & regards
Sid
AlanPosted Aug 22, 2008, 5:23 AM
Something like this perhaps (requires .NET 2.0 or later):
using System;
using System.IO;
class Program
{
static void Main()
{
string oldText = File.ReadAllText("somefile.txt");
string original = "this is sam ";
string replacement= "this is not sam ";
string newText = Parse(oldText, original, replacement);
File.WriteAllText("somefile2.txt", newText);
Console.WriteLine(newText);
Console.ReadKey();
}
static string Parse(string text, string original, string replacement)
{
replacement = "' " + original + " // original statement " + replacement + "// replaced ";
return text.Replace(original, replacement);
}
}