替换文本文件中的文本。 [英] Replacing text in a text file.
本文介绍了替换文本文件中的文本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试使用.Replace命令替换(显然)文本文件中的一行文本。我相信我使用sr.Readline.Replace()做错了,因为单击按钮时没有任何反应。我已经检查了剩余的代码
并使用不同的Then语句对其进行了调试。
如果有人有任何解决方案/建议,我们将不胜感激!
Imports System
Imports System.IO
Public Class Form1
Dim Oldname As String
Dim Newname As String
Dim file_location As String =" C:\Users\Jared\source \repos\Replace\Replace\bin\customers.txt.txt"
Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click
Oldname = txtOldname.Text
Newname = txtNewname.Text
使用sr As StreamReader = File.OpenText (file_location)
Do until sr.Peek = -1
如果Oldname.Contains(sr.ReadLine)那么
sr.ReadLine.Replace((Oldname),(Newname))
结束如果
循环
结束使用
结束子
....
结束等级
解决方案
js,
我没有测试过这个,但它应该关闭:
Private Sub _
FindAndReplace(ByVal textFilePath As String,_
ByVal findText As String,_
ByVal replaceText As String)
如果不是String.IsNullOrWhit eSpace(textFilePath)然后
Dim fi As New IO.FileInfo(textFilePath)
if fi.Exists Then
If Not String.IsNullOrWhiteSpace(findText)AndAlso _
Not String.IsNullOrWhiteSpace(replaceText)然后
Dim sb As New System.Text.StringBuilder
sb.Append(IO.File.ReadAllText(fi.FullName))
sb.Replace(findText,replaceText)
fi.Delete()
IO.File.WriteAllText(fi.FullName,sb.ToString)
End如果
结束如果
结束如果
结束子
Hi everyone,
I am trying to use the .Replace command to replace (obviously) one line of text in a text file with another. I believe I am doing it wrong by using sr.Readline.Replace() because nothing happens when the button is clicked. I have checked the rest of my code and debugged it using a different Then statement.
If anyone has any solutions/suggestions it would be greatly appreciated!
Imports System Imports System.IO Public Class Form1 Dim Oldname As String Dim Newname As String Dim file_location As String = "C:\Users\Jared\source\repos\Replace\Replace\bin\customers.txt.txt" Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Oldname = txtOldname.Text Newname = txtNewname.Text Using sr As StreamReader = File.OpenText(file_location) Do Until sr.Peek = -1 If Oldname.Contains(sr.ReadLine) Then sr.ReadLine.Replace((Oldname), (Newname)) End If Loop End Using End Sub .... End Class
解决方案
js,
I haven't tested this, but it should be close:
Private Sub _ FindAndReplace(ByVal textFilePath As String, _ ByVal findText As String, _ ByVal replaceText As String) If Not String.IsNullOrWhiteSpace(textFilePath) Then Dim fi As New IO.FileInfo(textFilePath) If fi.Exists Then If Not String.IsNullOrWhiteSpace(findText) AndAlso _ Not String.IsNullOrWhiteSpace(replaceText) Then Dim sb As New System.Text.StringBuilder sb.Append(IO.File.ReadAllText(fi.FullName)) sb.Replace(findText, replaceText) fi.Delete() IO.File.WriteAllText(fi.FullName, sb.ToString) End If End If End If End Sub
这篇关于替换文本文件中的文本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文