Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Replace specific line number

Posted on 2012-03-31
3
Medium Priority
?
456 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 86

Expert Comment

by:Mike Tomlinson
ID: 37792322
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 37792327
*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
ID: 37792424
Fantastic!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

926 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