vb.net 如何在 Visual Basic 中搜索文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15347527/
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
How to search a text file in Visual basic
提问by Alex Moneyy Amk
So my teacher wants the class to search a file for an authors name and display all the info on that author in a text box like a mailing label.
因此,我的老师希望全班在文件中搜索作者姓名,并在文本框中显示该作者的所有信息,如邮寄标签。
Here is my code i added a picture below. I'm really lost as to getting the program to take the Authors string and search another file. I can display the Authors name prefectly but how do you search another file for that name and display the information on that line about the author?
这是我的代码,我在下面添加了一张图片。对于让程序采用 Authors 字符串并搜索另一个文件,我真的很迷茫。我可以完美地显示作者姓名,但是您如何搜索另一个文件以查找该名称并在该行上显示有关作者的信息?
Imports System.IO
Public Class Form1
' CSCI 6
' Alex Smutny
' Lab #3
'
' DATE: 3/1/2013
Dim SR As IO.StreamReader
' initial start spot for the record Count
Dim RecordCount As Integer = 0
Dim Author As String = File.ReadAllText("Authors.txt")
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitBtn.Click
' Properly Exits the application.
Me.Close()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeBtn.Click
readBtn.Text = "Read"
SR.Close()
' Changes the button statuses Locked or unlocked and sets default settings.
openBtn.Enabled = True
quitBtn.Enabled = True
readBtn.Enabled = False
closeBtn.Enabled = False
SearBtn.Enabled = False
RecordCount = 0
recordNum.Clear()
bookNum.Clear()
isbnNum.Clear()
bookTitle.Clear()
authorName.Clear()
bookPrice.Clear()
bookQuanity.Clear()
reorderPoint.Clear()
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openBtn.Click
' Sets the file to read from and opens it.
SR = IO.File.OpenText("Books.Txt")
' Changes the status of the buttons again now that the File is open and ready to read.
openBtn.Enabled = False
quitBtn.Enabled = False
readBtn.Enabled = True
closeBtn.Enabled = True
SearBtn.Enabled = True
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readBtn.Click
'Changes the Read button to say next because it makes more since.
readBtn.Text = "Next"
' Setting all the variables.
Dim srIndex1 As Integer = 18
Dim srTitle As Integer
Dim srAuthor As Integer
Dim srPrice As Integer
Dim srDPrice As Double
Dim srData As String
Dim srLength As Integer
If SR.Peek() = -1 Then
MessageBox.Show("End of file")
Else
' Starts to read
srData = SR.ReadLine
' Increment counter by 1.
RecordCount = RecordCount + 1
' Determine the record length.
srLength = srData.Length
' gets the book number
bookNum.Text = srData.Substring(0, 3)
' ISBN number
isbnNum.Text = srData.Substring(4, 13)
' Book title
For srTitle = 1 To srLength Step 1
If srData.Substring(srIndex1 + srTitle, 1) = "," Then
bookTitle.Text = srData.Substring(srIndex1, srTitle)
srIndex1 = srIndex1 + srTitle + 1
srTitle = srLength + 1
End If
Next
' Book Author
For srAuthor = 1 To srLength Step 1
If srData.Substring(srIndex1 + srAuthor, 1) = "," Then
authorName.Text = srData.Substring(srIndex1, srAuthor)
srIndex1 = srIndex1 + srAuthor + 1
srAuthor = srLength + 1
Author = authorName.Text
End If
Next
' Book Price
For srPrice = 1 To srLength Step 1
If srData.Substring(srIndex1 + srPrice, 1) = "," Then
srDPrice = CDbl(srData.Substring(srIndex1, srPrice))
bookPrice.Text = srDPrice.ToString("C")
srIndex1 = srIndex1 + srPrice + 1
srPrice = srLength + 1
End If
Next
' Quanity
bookQuanity.Text = srData.Substring(srIndex1, 2)
srIndex1 = srIndex1 + 3
' Reorder Point
reorderPoint.Text = srData.Substring(srIndex1, 2)
End If
' record count
recordNum.Text = RecordCount
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
openBtn.Enabled = True
quitBtn.Enabled = True
readBtn.Enabled = False
closeBtn.Enabled = False
SearBtn.Enabled = False
End Sub
Private Sub SearBtn_Click(sender As System.Object, e As System.EventArgs) Handles SearBtn.Click
SR = IO.File.OpenText("Authors.Txt")
SR.Close()
readBtn.Text = "Restart"
RecordCount = 0
SR = IO.File.OpenText("Books.Txt")
End Sub
End Class
结束班


Authors File http://pastebin.com/t7C8ye9eBooks File http://pastebin.com/y6DNyUFd
作者文件http://pastebin.com/t7C8ye9e书籍文件http://pastebin.com/y6DNyUFd
So my goal is to just get the info for the current author and then get it from the other file and take out all trailing spaces and just display it as a mailing label.
所以我的目标是只获取当前作者的信息,然后从另一个文件中获取它并取出所有尾随空格并将其显示为邮件标签。
回答by Ares
There's a variety of good ways to do this. Let's, for now, assume that there's one author per line. If there is, you can get the entire string with My.Computer.FileSystem.ReadAllText()and use String.Split(Environment.NewLine)to split it on each new line. At any rate, you need to have some way to separate each author in the list. String.Splitreturns an array of String objects. You can then iterate through the array to determine if a term matches the search. Here's an example of a for loop.
有多种好方法可以做到这一点。现在,让我们假设每行有一个作者。如果有,您可以获取整个字符串My.Computer.FileSystem.ReadAllText()并使用String.Split(Environment.NewLine)它在每个新行上拆分它。无论如何,您需要有某种方式将列表中的每个作者分开。String.Split返回一个 String 对象数组。然后,您可以遍历数组以确定某个术语是否与搜索匹配。下面是一个 for 循环的例子。
'... search term given in param
'... after loading text file
Dim StrArr() As String
StrArr = AuthorsString.Split(Environment.NewLine)
Dim i as Integer
For i=0 to StrArr.Length - 1 Step 1
If StrArr(i).toLower = SearchTerm.toLower Then
'String is a match! Case insensitivity should make it a little easier for the user.
End If
Next
Let me know if you have any questions.
如果您有任何问题,请告诉我。

