vb.net 使用 Visual Basic 查找和替换 txt 文档中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10129288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:03:07  来源:igfitidea点击:

Find and replace text in a txt document using Visual Basic

vb.net

提问by Felix Arturo Macias Ibarra

Hi Im using Visual Basic 2008 Express Edition, my goal is to read an entire .txt file that works as a template and replace all ocurances of a word with a new one and save this new modified text in a new .txt when you press a command button. Can someone give me some tips?

嗨,我使用 Visual Basic 2008 Express Edition,我的目标是读取作为模板的整个 .txt 文件,并用新的单词替换所有出现的单词,并在您按下命令按钮。有人可以给我一些提示吗?

回答by Lajos Arpad

Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar")
My.Computer.FileSystem.WriteAllText("C:\test2.txt", fileReader, False)

So you should use the ReadAllText method of the FileSystem, pass the path as parameter and use the Replace method to replace what you want to replace. For more advanced usages of replace you can use regular expressions. You can read about that here.

所以你应该使用 FileSystem 的 ReadAllText 方法,将路径作为参数传递并使用 Replace 方法来替换你想要替换的内容。对于更高级的替换用法,您可以使用正则表达式。你可以在这里阅读。

Shorter version:

较短的版本:

My.Computer.FileSystem.WriteAllText("C:\test2.txt", My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar"), False)

回答by user2016683

I pulled this out of a a program I was making, just cut out the crap you don't need.

我从我正在制作的程序中提取了这个,只是去掉了你不需要的废话。

Private Sub UPDATECODES_Click(sender As Object, e As EventArgs) Handles UPDATECODES.Click
    Dim NEWCODE As String = NEWCODEBOX.Text
    Dim CC As String = CURRENTCODE.Text
    Dim oldcode As String
    Dim codelines(0 To 1) As String
    Dim olddate As String

    Using r1 As New System.IO.StreamReader(CODEDIR & "d.dat")
        olddate = r1.ReadToEnd
    End Using

    Using r2 As New System.IO.StreamReader(CODEDIR & "oc.dat")
        oldcode = r2.ReadToEnd
    End Using

    If System.IO.File.Exists(CODEDIR & "new code.txt") Then
        System.IO.File.Delete(CODEDIR & "new code.txt")
    End If

    If System.IO.File.Exists(CODEDIR & "CC.DAT") Then
        If IO.File.Exists(CODEDIR & "oc.dat") Then
            IO.File.Delete(CODEDIR & "OC.DAT")
        End If

        My.Computer.FileSystem.RenameFile(CODEDIR & "CC.DAT", "OC.DAT")
        Dim FILESTREAM As System.IO.FileStream
        FILESTREAM = New System.IO.FileStream(CODEDIR & "CC.DAT", System.IO.FileMode.Create)
        FILESTREAM.Close()
    End If

    Using WRITER As New System.IO.StreamWriter(CODEDIR & "CC.DAT")
        WRITER.WriteLine(NEWCODE)
    End Using

    Dim currentlines(0 To 1) As String
    Dim a As Integer
    Dim TextLine(0 To 1) As String
    a = 0

    Using sr As New System.IO.StreamReader(CODEDIR & "internet code.txt")
        While Not sr.EndOfStream
            ReDim codelines(0 To a)
            codelines(a) = sr.ReadLine()
            codelines(a) = codelines(a).Replace(CURRENTCODE.Text, NEWCODE)
            codelines(a) = codelines(a).Replace(olddate, DATEBOX1.Text & " - " & DATEBOX2.Text)
            Dim newfile As String = (CODEDIR & "new code.txt")
            Using sw As New System.IO.StreamWriter(newfile, True)
                sw.WriteLine(codelines(a))
            End Using
            a = a + 1
        End While
    End Using

    Using sw2 As New System.IO.StreamWriter(CODEDIR & "d.dat")
        sw2.WriteLine(date1 & " - " & date2)
    End Using

    System.IO.File.Delete(CODEDIR & "internet code.txt")
    My.Computer.FileSystem.RenameFile(CODEDIR & "new code.txt", "internet code.txt")
    CURRENTCODE.Text = NEWCODE
End Sub

回答by Wayne

I have a small program I wrote that I use for reusing code. I use it when I need to create a class, or code file, that is almost identical to an existing one. It is a basic form program with four text boxes and a button that uses the same command as the accepted answer except the strings are not hard coded.

我编写了一个小程序,用于重用代码。当我需要创建一个几乎与现有文件相同的类或代码文件时,我会使用它。它是一个带有四个文本框和一个按钮的基本表单程序,除了字符串不是硬编码之外,它使用与接受的答案相同的命令。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

  Dim source As String = txtSource.Text
  Dim destination As String = txtDestination.Text
  Dim oldText As String = txtOldText.Text
  Dim newText As String = txtNewText.Text

  My.Computer.FileSystem.WriteAllText(destination, My.Computer.FileSystem.ReadAllText(source).Replace(oldText, newText), False)

End Sub

回答by COLD TOLD

try the following example I hope it helps

试试下面的例子我希望它有帮助

 Dim lOpenFile As Long
 Dim sFileText As String
 Dim sFileName As String

sFileName = "C:\test.txt"

 'open the file and read it into a variable
 lOpenFile = FreeFile
 Open sFileName For Input As lOpenFile
 sFileText = Input(LOF(lOpenFile), lOpenFile)
 Close lOpenFile

'change 'John Doe' to 'Mary Brown'
 sFileText = Replace(sFileText, " John Doe ", " Mary Brown ")

 'write it back to the file
 lOpenFile = FreeFile
 Open sFileName For Output As lOpenFile
 Print #lOpenFile, sFileText
 Close lOpenFile