如何在VB,Net中分析文本文档的值?

时间:2014-11-17 23:09:03

标签: vb.net parsing text-files

我希望将此文本文件解析为字符串,以将其插入数据库。

源文本文件被读取为以下字符串:

Line of unwanted text
Another line of unwanted data

Timestamp: 1/1/10 12:00 PM 
ID: 1 
Details: All data processed. Length will vary.

我想只读取时间戳,ID和详细信息,并将它们放在单独的字符串中以将它们插入数据表中。什么是在行之后捕获所有内容的最佳方法?到行尾?

Dim Details as String = TextFile.Substring(Message.IndexOf("Details:"), X)

2 个答案:

答案 0 :(得分:1)

假设您的文件完美无缺......一种方法:

Imports System.IO

    Dim AllLines() As String = File.ReadAllLines(FilePath)
    Dim DatasIndex As Int32 = -1

    For i As Int32 = 0 To AllLines.Length - 1
        If AllLines(i).StartsWith("T") OrElse AllLines(i).StartsWith("t") Then
            If AllLines(i).ToUpper().StartsWith("TIMESTAMP: ") Then
                DatasIndex = i
                Exit For
            End If
        End If
    Next

    If DatasIndex > -1 Then
        ' Dim ReadDate As Date = Date.Parse(AllLines(DatasIndex).Substring(11))
        ' Dim ReadID As Int32 = Integer.Parse(AllLines(DatasIndex + 1).Substring(4))
        Dim ReadDate As String = AllLines(DatasIndex).Substring(11)
        Dim ReadID As String = AllLines(DatasIndex + 1).Substring(4)
        Dim ReadDetails As String = AllLines(DatasIndex + 2).Substring(9)

        ' send to database
    End If

您没有告诉Timestamp:ID:Details:字符串是否始终采用相同的顺序,并且每个属性名称后面都有一个尾随空格。

答案 1 :(得分:1)

如果您必须使用String作为输入,则可以使用String.Split将其分解为行,并处理每一行。 String.Substring可用于提取剩下的行 - 我刚刚对下面的起始位置进行了硬编码。

Dim timestamp As String = Nothing
Dim id As String = Nothing
Dim details As String = Nothing
For Each line In input.Split({vbCrLf, vbCr, vbLf}, StringSplitOptions.None)
    If line.StartsWith("timestamp:", StringComparison.OrdinalIgnoreCase) Then
        timestamp = line.Substring(10).Trim()
    ElseIf line.StartsWith("id:", StringComparison.OrdinalIgnoreCase) Then
        id = line.Substring(3).Trim()
    ElseIf line.StartsWith("details:", StringComparison.OrdinalIgnoreCase) Then
        details = line.Substring(8).Trim()
    End If
Next

如果您可以更改读取数据的方式,那么循环可能只是:

For each line In File.ReadLines("your\file\name.txt")
    ...
Next