Solved

C# Replace specific line number

Posted on 2012-03-31
3
447 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
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 85

Accepted Solution

by:
Mike Tomlinson earned 500 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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