Solved

Search and replace string in a file using ASP.NET C#

Posted on 2009-07-02
7
682 Views
Last Modified: 2012-05-07
Hi,

I am new to ASP.NET C#.

I want to search for a string and replace that string with another string in a file.
How do I do that?
0
Comment
Question by:meetpd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24770402
Replace String Examples in C#
http://dotnetperls.com/replace-string-use
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24770405
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 200 total points
ID: 24770553
try this code
   StreamReader sr;
            StreamWriter sw;
            string strData;
             sr = new StreamReader(@"C:\Test2.txt");
                                    strData = sr.ReadToEnd();
                                    sr.Close();
                                    sw = new StreamWriter(@"C:\Test2.txt");
                                 strData =   strData.Replace("Old", "New"); 
                                
                                    sw.Write(strData);
                                    sw.Flush();
                                    sw.Close();
            sr.Dispose();
            sw.Dispose();

Open in new window

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 3

Assisted Solution

by:coagantus
coagantus earned 100 total points
ID: 24770571
this function get 3 parameters filepath, searchText and replaceText. pas it your required parametrs and enjoy...
static public void ReplaceInFile( string filePath, string searchText, string replaceText )
{
    StreamReader reader = new StreamReader( filePath );
    string content = reader.ReadToEnd();
    reader.Close();
 
    content = Regex.Replace( content, searchText, replaceText );
 
    StreamWriter writer = new StreamWriter( filePath );
    writer.Write( content );
    writer.Close();
}

Open in new window

0
 

Author Comment

by:meetpd
ID: 24771556
How do I replace

<?xml version="1.0"?> with
<?xml version="1.0" encoding="ISO-8859-1" ?>

Because there are double quotes in between, I am not able to figure out how to put them in

  strData =   strData.Replace("Old", "New")   format.

Can you please help?


0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 24771566
You need to use an escape-char

strData =   strData.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>")   format.
0
 

Author Comment

by:meetpd
ID: 24771685
Thanks Everyone for helping me you...

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question