BrunoEarth BrunoEarth - 4 years ago 81
C# Question

How to remove text from a specific line in file without creating unwanted space using c#?

I'm trying to remove text from a specific line in the file that i have created "Test.txt".

Here is the text written in Test.txt:

1

2

3

4

5


1

2

3

4

5


1

2

3

4

5


Now what i want to happen is when i'm going to remove 2 - 4 in the 2nd iteration, the output should be like this:

1

2

3

4

5


1

5


1

2

3

4

5


What i want is to remove only spaces that the corresponding text was removed from and preferably without involving other spaces.
But when i tried the code it gave me this output:

1

2

3

4

5


1







5


1

2

3

4

5


As you can see the above output, that is the unwanted space that i'm talking about. Below is the code that I have tried:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
class Program2
{
static void Main()
{
lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 8);
lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 9);
lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 10);

}

static void lineChanger(string newText, string fileName, int line_to_edit)
{
string[] arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
}
}
}


Also please feel free to suggest any proper techniques and kindly include actual examples.

Answer Source

Please see is this ok?

        int removeAt = 7; //or any thing you want
        int linesToRemove = 3; //or any thing you want
        string s = System.IO.File.ReadAllText("Test.txt");
        List<string> arr = s.Split("\n".ToCharArray()).ToList();

        for (int i = 0; i < linesToRemove; i++)
            arr.RemoveAt(removeAt);
        string result = "";
        foreach (string str in arr)
        {
            result += str;
        }
        System.IO.File.WriteAllText("Test.txt", result);

I'm happy that you say it works for you, and here i add another solution, maybe in some cases you need to use this:

        int removeAt = 7; //or any thing you want
        int linesToRemove = 3; //or any thing you want
        string s = System.IO.File.ReadAllText("Test.txt");
        List<string> arr = s.Split(new char[] { '\n' }).ToList();
        List<int> realBareRows = new List<int>();
        for (int i = 0; i < arr.Count; i++)
        {
            if (string.IsNullOrEmpty(arr[i].Replace("\r", "")))
                realBareRows.Add(i);
        }
        List<string> newArr = s.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
        foreach (int j in realBareRows)
            newArr.Insert(j, "\n");

        for (int i = 0; i < linesToRemove; i++)
            newArr.RemoveAt(removeAt);

        string result = "";
        foreach (string str in newArr)
        {
            result += str + System.Environment.NewLine;
        }
        System.IO.File.WriteAllText("Test.txt", result);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download