0

Could anyone tell me how to edit a specific line in a text document? For instance, lets say that my document contains two phone numbers:

"0889367882

0887343160"

I want to delete the second number and write a new phone number, how can I do that? I am printing the text in the document, but i don't know how to choose which line to edit and how to do that.

    string path = @"C:\Users\...\text1.txt";
    string[] lines = File.ReadAllLines(path);
    int i = 0;

    foreach (var line in lines)
    {
        i++;
        Console.WriteLine("{0}. {1}", i, line);
    }

Thanks!

3 Answers3

1

Simply use string.replace.

Like this:

if(line.Contains("0887343160")
    line = line.Replace("0887343160", "0889367882");

and after replacing, write all lines back in the file.

Shaharyar
  • 11,393
  • 3
  • 39
  • 59
  • 2
    This is just a replace process. How about reading specific line in a text and changing it? Your answer is not complete. – Soner Gönül Dec 25 '13 at 08:30
  • He has already read all the lines from file, now he can change the specific line using this code. – Shaharyar Dec 25 '13 at 08:31
  • I want the user to choose which line to edit and how to edit it. –  Dec 25 '13 at 08:33
  • You have an array of `lines`. You may ask user to input the line number he wants to edit and then edit that line directly by using `index`. The editing code is just one as I mentioned. use it as you need. – Shaharyar Dec 25 '13 at 08:36
  • @user3124732 Shaharyar has used just static value which you mentioned. You can pass dynamic values using string variable, which will contain the user entered value. – Naveen Dec 25 '13 at 08:37
  • used that: int n = int.Parse(Console.ReadLine()); lines[n] = lines[n].Replace(lines[n], Console.ReadLine()); worked perfectly –  Dec 25 '13 at 08:48
  • @user3124732 exactly what I meant – Shaharyar Dec 25 '13 at 09:27
1

A better version would be to iterate the lines in the file rather than loading the whole file lines to memory. Hence using an iterator would do best here.

We do a MoveNext() on the iterator object and write the current line pointed by the iterator to the file after executing the necessary replace logic.

StreamWriter wtr = new StreamWriter("out.txt");
var e = File.ReadLines(path).GetEnumerator();
int lineno = 12; //arbitrary
int counter = 0;
string line = string.Empty;
while(e.MoveNext())
{
    counter++;
    if(counter == lineno)   
        line = replaceLogic(e.Current); 
    else
        line = e.Current;
    wtr.WriteLine(line);
}
wtr.Close();
deostroll
  • 10,935
  • 19
  • 77
  • 147
0

Solution 1: if you want to remove the Line based on user input String (matches with one of the line from file) you can try this.

        string path = @"C:\Data.txt";
        string[] lines = File.ReadAllLines(path);
        String strRemove = "8971820518";
        List<String> lst = new List<String>();
        for(int i=0;i<lines.Length;i++)
        {
            if (!lines[i].Equals(strRemove))  //if string is part of line use Contains()
            {
                lst.Add(lines[i]);
            }
        }
        File.WriteAllLines(path,lst.ToArray());

Solution 2: if you want to remove the Line based on user input LineNO (matched with exact line no in file) you can try this

        string path = @"C:\Data.txt";
        string[] lines = File.ReadAllLines(path);
        int  iRemoveLineNo = 6;
        List<String> lst = new List<String>();
        for(int i=0;i<lines.Length;i++)
        {
            if (iRemoveLineNo-1!=i)
            {
                lst.Add(lines[i]);
            }
        }
        File.WriteAllLines(path,lst.ToArray());
Sudhakar Tillapudi
  • 24,787
  • 5
  • 32
  • 62