简体   繁体   English

如何使用VB.NET过滤TXT文件?

[英]How can I filter a TXT file using VB.NET?

I have a txt file that displays the following information returned by AutoCAD: 我有一个txt文件,其中显示了AutoCAD返回的以下信息:

; IAcadToolbar: An AutoCAD toolbar

; Property values:

;   Application (RO) = #<VLA-OBJECT IAcadApplication 00d591b4>

;   Count (RO) = 19

;   DockStatus (RO) = 4

;   FloatingRows = 5

;   Height (RO) = AutoCAD: The toolbar is invisible. Please make it visible

;   HelpString = "Draw Toolbar\n    "

;   LargeButtons (RO) = 0

;   left = 1310

;   Name = "Draw"

;   Parent (RO) = #<VLA-OBJECT IAcadToolbars 224a6b04>

;   TagString (RO) = "ID_TbDraw"

;   top = 646

;   Visible = 0

;   Width (RO) = AutoCAD: The toolbar is invisible. Please make it visible

; Methods supported:

;   AddSeparator (1)

;   AddToolbarButton (5)

;   Delete ()

;   Dock (1)

;   Float (3)

;   Item (1)

Problem being is that I need to filter it so I am only left with: 问题是我需要过滤它,所以我只剩下:

;   DockStatus (RO) = 4

;   left = 1310

;   Name = "Draw"

;   top = 646

;   Visible = 0

They must also remain in the same order: 它们还必须保持相同的顺序:

; DockStatus (RO) = 4 ; DockStatus (RO) = 4 always on top followed by ; left = 1310 ; DockStatus (RO) = 4总是在最前面,然后是; left = 1310 ; left = 1310 and so on, all other information may be discarded. ; left = 1310 ,依此类推,所有其他信息都可能被丢弃。

Dose anybody know how to do this in VB.NET? 有人知道如何在VB.NET中做到这一点吗?

Imports System.IO
Imports System.Text

Dim output = New StringBuilder()

' Read the lines from FileName into an array of strings. '
Dim input = File.ReadAllLines(FileName)

For Each line in input

    If line.StartsWith(";   DockStatus (RO) = ") OrElse
       line.StartsWith(";   left = ") OrElse
       line.StartsWith(";   Name = ") OrElse
       line.StartsWith(";   top = ") OrElse
       line.StartsWith(";   Visible = ") Then

       output.AppendLine(line)

    End If

Next

Ok, assuming that you've got a file that you're reading line by line, can't you simply do something like this? 好的,假设您有一个要逐行读取的文件,难道您不能简单地做这样的事情吗? (pseudo code only) (仅限伪代码)

string result;
foreach line in linesFromFile
{
    if(line.StartsWith("; DockStatus") or 
       line.StartsWith("; Name = "))
    {
        result += line;
    }
}

What I've presented is not pretty, but might get you started. 我介绍的内容并不漂亮,但可能会让您入门。

我建议将所有值都放在字符串中,然后使用分割器,然后过滤掉所需的值,并对它们进行排序,以便在需要时将它们转换为所需的输出格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM