Solved

C# Replace specific line number

Posted on 2012-03-31
3
449 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
[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
  • 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 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

739 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