這是我在Yaoo知識+回覆的資料,
以下為vb.net內與文字檔操作有關的語法,
我就把他貼在這兒了。
1.搜尋字串
Function Get_str()
Dim i As Integer = 0
FileOpen(1, "c:\要搜尋的文字檔.txt", OpenMode.Input)
Do Until EOF(1)
i = i + 1
If InStr(LineInput(1), "要找的字串") Then
MsgBox("要找的字串在第: " & i & " 行")
FileClose(1)
Exit Function
End If
Loop
MsgBox("找不到此字串!")
End Function
2.
如果是想要找出a.txt的bcb,並且拷貝到b.txt內
您可以這樣做
Dim sr As New System.IO.StreamReader("c:\a.txt", System.Text.Encoding.Default)
Dim sw As New System.IO.StreamWriter("c:\b.txt", False, System.Text.Encoding.Default)
Do While sr.Peek > -1
Dim t As String = sr.ReadLine
If t.Substring(0, 3) = "bvb" Then
sw.WriteLine(t)
End If
Loop
sw.Close()
sr.Close()
3.找出a.txt內的ab拷貝到b.txt,並且改為p2:
Dim sr As New System.IO.StreamReader("c:\a.txt", System.Text.Encoding.Default)
Dim sw As New System.IO.StreamWriter("c:\b.txt", False, System.Text.Encoding.Default)
Do While sr.Peek > -1
Dim t As String = sr.ReadLine
If t.Substring(0, 2) = "ab" Then
sw.WriteLine("p2" & t.Substring(2))
End If
Loop
sw.Close()
sr.Close()
請注意這裡的括弧是全形的,拷貝到vb.net內之後記得改為半形喔!
沒有留言:
張貼留言