我在Microsoft Visual Basic中创建了一个软件。我只需要最后一件事就是让软件通过文档搜索并找到两个给定数字之间的值。我正在从烟雾探测器打印出日志文件,当你有超过一千个它们时,它就更容易扫描日志文件。
每个烟雾探测器都有一个"框"信息,但它是信息" verdi ="这很有趣。
示例:
Adresse 01.001 sone 0007 SFU.3ETG.TRAPP 1 r›ykdetektor, optisk Dc verdi=23 F›lsomhets modus=5 Dato=2009/00 Godkj.=35 Drift=16 Adresse 01.002 sone 0007 SFU.3ETG.GANG V/TRAPP 1 r›ykdetektor, optisk Dc verdi=23 F›lsomhets modus=5 Dato=2009/00 Godkj.=35 Drift=18
在这里你可以看到这两个烟雾探测器的值都在23" verdi = 23"。所以我希望软件简单地列出两个给定数字中的所有值,ex;较低的价值" verdi = 30"和上限值" verdi = 40"。
以下是代码:
Public Class choosedocument
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs)
End Sub
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
RichTextBox1.Text = ""
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
RichTextBox1.Undo()
End Sub
Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
RichTextBox1.Redo()
End Sub
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
RichTextBox1.Cut()
End Sub
Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
RichTextBox1.Copy()
End Sub
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
RichTextBox1.Paste()
End Sub
Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
RichTextBox1.SelectAll()
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
On Error GoTo E
OpenFileDialog1.ShowDialog()
Dim readit As New System.IO.StreamReader(OpenFileDialog1.FileName)
RichTextBox1.Text = readit.ReadToEnd
readit.Close()
Exit Sub
E:
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
On Error GoTo E
SaveFileDialog1.ShowDialog()
Dim writeit As New System.IO.StreamWriter(SaveFileDialog1.FileName)
writeit.Write(RichTextBox1.Text)
writeit.Close()
Exit Sub
E:
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub Scan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles scanbutton.Click
End Sub
Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
End Sub
End Class
答案 0 :(得分:1)
试试这个:
Dim allLines As String() = File.ReadAllLines("input_file.txt")
Dim allReadings As List(Of String) = allLines.Where(Function(line) line.Contains("verdi")).[Select](Function(line) line.Split(New [String]() {"verdi="}, StringSplitOptions.None)(1).Split(" "C)(0)).ToList()
答案 1 :(得分:0)
感谢大家的帮助。我从某人那里得到了答案,并认为把它放在这里会很好。
if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
string start = "verdi=" + lowervaluebox.Text;
string end = "verdi=" + uppervaluebox.Text;
richTextBox1.AppendText(
string.Join("",
File.ReadLines(openFileDialog1.FileName)
.Where(x => x.CompareTo(start) >= 0
&& x.CompareTo(end) <= 0)
.Select(x => x + "\n")
.ToArray()));