Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 基于文本和位置将文本字符串解析为字段_Vb.net - Fatal编程技术网

Vb.net 基于文本和位置将文本字符串解析为字段

Vb.net 基于文本和位置将文本字符串解析为字段,vb.net,Vb.net,我有一个由空格分隔的字符串组成的.text文件。当我将其放入Excel或Access时,这将是显而易见的解析解决方案,但由于字符串大小不同,字段将不正确。我希望能在这里找到一些帮助,因为我在这方面还没有真正的经验 这是字符串的一个示例部分: 25999887575 1500:14:38C330:14:42T3300:14:52 00:14:58 202224446898 3300:16:24B23 00:17:00 C3100:17:15T31 00:19:30 C04 00:17:15 T28

我有一个由空格分隔的字符串组成的.text文件。当我将其放入Excel或Access时,这将是显而易见的解析解决方案,但由于字符串大小不同,字段将不正确。我希望能在这里找到一些帮助,因为我在这方面还没有真正的经验

这是字符串的一个示例部分:

25999887575 1500:14:38C330:14:42T3300:14:52 00:14:58

202224446898 3300:16:24B23 00:17:00 C3100:17:15T31 00:19:30 C04 00:17:15 T2800:19:30 00:32

带有冒号的数字是时间戳,字母代码(T/C/B)都表示不同的字段类型。我的问题是字符串中可以列出任意数量的C和T时间戳,并且可能有也可能没有B时间戳

我希望结果显示4个字段。。。第一个、第一个c时间戳、最后一个t时间戳和最后一个时间戳(粗体时间戳)。所有其他信息都可以忽略不计。我想用VB来循环通过由于记录的数量


感谢您的帮助

编辑:似乎您想将日期分成不同的类型,由每个字母前的位决定。
可以使用Strings.Split方法在空格处分割线。然后使用Date.TryParse方法检查哪些部分是日期(或时间,相同)。然后选择上一部分的第一个字母,并将日期排序到各自的列表中

    Dim s As String = "202224446898 33 00:16:24 B23 00:17:00 C31 00:17:15 T31 00:19:30 C04 00:17:15 T28 00:19:30 00:19:32"
    Dim parts() As String = Strings.Split(s, " ") 'Split the string at all spaces

    'Create lists of Date, one list for all dates, one list for each Letter
    Dim Tdates As New List(Of Date) 'This stores converted dates
    Dim Cdates As New List(Of Date) 'This stores converted dates
    Dim Bdates As New List(Of Date) 'This stores converted dates
    Dim Alldates As New List(Of Date) 'This stores converted dates

    For i = 0 To parts.Count - 1 'Iterate through all parts
        Dim ThisDate As Date
        If Date.TryParse(parts(i), ThisDate) = True Then 'This tries to parse the current part as a date, returns True if the part is a date
            Alldates.Add(ThisDate) 'You want to store all dates in this list
            If i > 0 AndAlso parts(i - 1).Length > 0 Then
                Dim PrevPartsFirstLetter As String = parts(i - 1)(0) 'Crop the first letter of the previous part
                Select Case PrevPartsFirstLetter
                    Case "T" : Tdates.Add(ThisDate)
                    Case "C" : Cdates.Add(ThisDate)
                    Case "B" : Bdates.Add(ThisDate)
                End Select
            End If
        End If
    Next
    'You can now select the parts you want from the lists

Tdates列表包含前面有T的所有日期,依此类推。AllDates列表包含行中的所有日期。

如果您手动将这两条记录转换为您要查找的格式,包括您要使用的字段名,则会有所帮助。一旦你完成了,我可以帮你解析。没关系。。。看起来@jens并不像我那个样目不转睛。我们会看看OP是否想要那个,还不太清楚。