例如,我想在文本檔案中只修改文本框中 [newtext] 103 和 [endtext] 103 之間的內容。
[newtext]101
This is a first demonstration
This is a message
of Hello World
[endtext]101
[newtext]102
This is a 2nd demonstration
This is a 2nd message
of Hello World
[endtext]102
[newtext]103
This is a 3nd demonstration
This is a 3nd message
of Hello World
[endtext]103
所以如果我的文本框中有文本
This is a newest demonstration
This is a newest message
of Hello World
我如何適應用新文本替換舊文本?也就是說,輸出將是:注意,之間的其他值[newtext] 101, [newtext] 102,不會被擦除。
[newtext]103
This is a newest demonstration
This is a newest message
of Hello World
[endtext]103
這是一個代碼,但不幸的是它必須找到所選引數之間的值。
Dim text As String = File.ReadAllText(My.Application.Info.DirectoryPath ("\Data.txt"))
text = text.Replace(TextBox1.Text, TextBox2.Text)
File.WriteAllText("Data.txt", text)
uj5u.com熱心網友回復:
ReadAllLines回傳文本檔案中的行陣列。獲取包含搜索文本的行的索引。然后獲取TextBox1. 只需將新文本分配給檔案行中的適當索引即可。最后,我顯示了新的 string TextBox2。
確保兩個文本框都足夠寬以顯示行并且 Multiline = True。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileLines = File.ReadAllLines("C:\Desktop\Code\DemoMessage.txt")
Dim lineToFind = "[newtext]103"
Dim index = Array.IndexOf(FileLines, lineToFind)
Dim tbLines = TextBox1.Lines
FileLines(index 1) = tbLines(0)
FileLines(index 2) = tbLines(1)
FileLines(index 3) = tbLines(2)
TextBox2.Text = String.Join(Environment.NewLine, FileLines)
'you can write the contents of TextBox2 back to file if needed.
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/341376.html
標籤:网络
