Solved

C# Replace specific line number

Posted on 2012-03-31
3
438 Views
Last Modified: 2012-03-31
How can I use this method to receive an integer index number and then replaces the string in that particular line:

public void replace (int index)
        {

             try
            {
                String strFile = File.ReadAllText("c:\\test.txt");

           
                if (strFile.Contains("A"))
                        {
                            strFile = strFile.Replace("P", "A");
                            strFile = strFile.Replace(" ", ";");

                            File.WriteAllLines("c:\\test.txt", strFile[index]);
                        
                        }
     
                        
                        

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

Open in new window

0
Comment
Question by:pauledwardian
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Something like:
        public void replace(int index) // <-- zero based index: line #1 is at index 0 (zero)
        {
            try
            {
                String[] lines = File.ReadAllLines("c:\\test.txt");
                if (lines.Length > index)
                {
                    lines[index] = lines[index].Replace("P", "A").Replace(" ", ";");
                    File.WriteAllLines("c:\\test.txt", lines);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
*Oops...I forgot to add your check for "A" back in:
        public void replace(int index) // <-- zero based index: line #1 is at index 0 (zero)
        {
            try
            {
                String[] lines = File.ReadAllLines("c:\\test.txt");
                if (lines.Length > index)
                {
                    if (lines[index].Contains("A"))
                    {  
                        lines[index] = lines[index].Replace("P", "A").Replace(" ", ";");
                        File.WriteAllLines("c:\\test.txt", lines);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Open in new window

0
 

Author Closing Comment

by:pauledwardian
Comment Utility
Fantastic!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Total in textboxes 9 34
Separate a number and letter from a string 25 23
SQl help with selection 14 43
Setting runtime form location 4 18
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now