none
how to replace a word in specific line in text file RRS feed

  • Question

  • in text file i have multiline

    so i want in of each line where "faux" will be replaced with "ok"

    1 abc abc abc 5 6  faux

    2 abc abc abc 5 6 faux

    with the number that i have i will change faux with ok

    something like this but with point to the number that i put it in first place of line

    My.Computer.FileSystem.WriteAllText(TextBox1.Text, My.Computer.FileSystem.ReadAllText(TextBox1.Text).Replace("faux", "ok"), False)


    Tuesday, June 16, 2020 3:25 PM

All replies

  • Hi

    I tried your code line.  I used a text file containing:

    *

    Here is some faux text with faux contained in the flow faux of characters where faux will be replaced faux with 'ok' as requested. faux

    *

    After running the text file contained:

    *

    Here is some ok text with ok contained in the flow ok of characters where ok will be replaced ok with 'ok' as requested. ok

    *

    So, what problems are you having. I can't help but think Iam misunderstanding your question. I realise that your written English is not your first language, but I think it is the cause of my misunderstanding.

    *

    You make a reference to 'numbers' - but which numbers, and what is their relevance?

    *

    Here is the code I tested with

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Testing.txt")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        My.Computer.FileSystem.WriteAllText(TextBox1.Text, My.Computer.FileSystem.ReadAllText(TextBox1.Text).Replace("faux", "ok"), False)
    
      End Sub
    End Class

    This was starting contents of the Text file:

    Here is some faux text with
    faux contained in the flow faux
    of characters where faux will
    be replaced faux with 'ok' as
    requested. faux

    and the contents after running code

    Here is some ok text with
    ok contained in the flow ok
    of characters where ok will
    be replaced ok with 'ok' as
    requested. ok


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, June 16, 2020 4:07 PM
    Tuesday, June 16, 2020 4:03 PM
  • thanks for answering

    i want when i complete some task it goes to the specified line and change that faux to ok and continue for exemple i have 3 line

    1 abc abc .... faux

    2 abc abc ......faux

    3 abc abc ...... faux

    when complete the loop and all work return to the line in the text file with exact number and replace it with ok

    like replace the faux in line 2 with ok

    or line 3 with ok and so on

    i hope this making things clear

     

    Tuesday, June 16, 2020 4:22 PM
  • Hi

    Please show text file contents before process, and text file after process.

    I do not see that mycode is different from your last post explanation.


    Regards Les, Livingston, Scotland

    Tuesday, June 16, 2020 4:36 PM
  • It is guessing, but this changes faux in the second line

            Dim TheLines = Split(TextBox1.Text, Environment.NewLine)
            TheLines(1) = TheLines(1).Replace("faux", "OK") 'changes the second (2) line'
            TextBox1.Text = String.Join(Environment.NewLine, TheLines)


    Success
    Cor

    Tuesday, June 16, 2020 5:25 PM