1

I got a text file with e.g. 3 lines:

Example Text
Some text here
Text

I want to add some text directly after "here", so it will look like this:

Example Text
Some text hereADDED TEXT
Text

My code, so far, looks like this, I used some of the code from here, but it doesn't seem to work.

List<string> txtLines = new List<string>();

string FilePath = @"C:\test.txt";

foreach (string s in File.ReadAllLines(FilePath))
{
    txtLines.Add(s);
}

txtLines.Insert(txtLines.IndexOf("here"), "ADDED TEXT");

using (File.Create(FilePath) { }

foreach (string str in txtLines)
{
    File.AppendAllText(FilePath, str + Environment.NewLine);
}

My problem is: txtLines.IndexOf("here") returns -1, thus throwing a System.ArgumentOutOfRangeException.

Can somebody tell me what I am doing wrong?

Urist McDev
  • 498
  • 3
  • 14
jacobz
  • 3,191
  • 12
  • 37
  • 61
  • The problem is, that IndexOf checks for the entry in your list, not for the string within this entry. This said you have to check every ntry within this list (via any kind of loop) and check if your searched word is within this entry. This means you will need to find the entry first, and afterwards the position within this entry – MakePeaceGreatAgain Oct 10 '13 at 10:19
  • Do you want to just replace the first instance of the text `here`? If not, must it only be replaced if it is on line 2? What about if there's a word like `there`? Should that be replaced with `whereADDED TEXT`? – Matthew Watson Oct 10 '13 at 10:23
  • If I want to replace the last instance of it, do I have to use `LastIndexOf` then? – jacobz Oct 10 '13 at 10:27

3 Answers3

3

Is there a reason your loading all of your text into a List? You could just update the values as you read them from the file.

        string FilePath = @"C:\test.txt";

        var text = new StringBuilder();

        foreach (string s in File.ReadAllLines(FilePath))
        {
            text.AppendLine(s.Replace("here", "here ADDED TEXT"));
        }

        using (var file = new StreamWriter(File.Create(FilePath)))
        {
            file.Write(text.ToString());
        }
pingoo
  • 2,074
  • 14
  • 17
  • What if we want to add more lines with out duplicating prior lines twice, I see this works but what is happening is I am trying to replace all the lines first then append it at the end how can we accomplish this. – shawn Aug 24 '15 at 21:09
0

Here is a piece of code that should help you. Just replace your line txtLines.Insert(txtLines.IndexOf("here"), "ADDED TEXT"); with the below. It finds the first here and replaces it with hereADDED TEXT:

int indx=txtLines.FindIndex(str => str.Contains("here"));
txtLines[indx]= txtLines[indx].Replace("here", "hereADDED TEXT");
ttitto
  • 97
  • 1
  • 10
-2
            string filePath = "test.txt";
            string[] lines = File.ReadAllLines(FilePath);
            for (int i = 0; i < lines.Length; i++)
            {
                lines[i] = lines[i].Replace("here", "here ADDED TEXT");
            }

            File.WriteAllLines(filePath, lines);

It will do the trick that you want.

Rezoan
  • 1,745
  • 22
  • 51