我有一个文本文件,其中包含如下所示的音乐列表:
BeginSong{
Song Name
Artist
Genre
}EndSong
有多个这样的实例。
我想在BeginSong{
和}EndSong
之间获取文字并输入歌曲信息
成一个字符串数组。然后我想将每个实例添加到ListBox作为艺术家 - 歌曲名称
(那部分我确定我可以搞清楚)。我希望这是一个清晰的描述。
答案 0 :(得分:1)
使用ReadLine()
FileStream
功能
由于您已经知道了信息的顺序,因此您应该能够循环所有文件行并将它们存储在相应的属性中。
伪:
WHILE Reader.Read()
Store Line in BeginSongTextVariable
Read Next Line
Store Line in SongNameVariable
Read Next Line
Store Line in ArtistNameVariable
Read Next Line
Store Line in GenreVariable
Read Next Line
Store Line in EndSongTextVariable
Add The Above Variables in List
End While
答案 1 :(得分:0)
您可以使用带有命名组的正则表达式:
BeginSong{\n(?<song_name>.*)\n(?<artist>.*)\n(?<genre>.*)\n}EndSong
这样的事情:
Imports System.Text.RegularExpressions
'...
Dim s As New Regex("BeginSong{\n(?<song_name>.*)\n(?<artist>.*)\n(?<genre>.*)\n}EndSong")
Dim mc As MatchCollection = s.Matches(inputFile)
For Each m As Match In mc
Dim song_name As String = m.Groups("song_name").Value
Dim artist As String = m.Groups("artist").Value
Dim genre As String = m.Groups("genre").Value
'use or store these values as planned
Next
答案 2 :(得分:0)
Neolisk有一个很好的答案,它使用正则表达式。但是既然除了VB.NET之外你还包括了VB6标签,我将采用VB6解决方案。
您可以使用字符串Split功能,并在“结束”上拆分,即"BeginSong{"
和"}EndSong"
Dim songInfos As String
Dim firstArray() As String
Dim secondArray() As String
Dim thirdArray() As String
Dim songInfoArray() As String
Dim i As Integer
Dim songCounter As Integer
' to test:
songInfos = songInfos & "BeginSong{" & vbNewLine
songInfos = songInfos & "Song Name1" & vbNewLine
songInfos = songInfos & "Artist1" & vbNewLine
songInfos = songInfos & "Genre1" & vbNewLine
songInfos = songInfos & "}EndSong" & vbNewLine
songInfos = songInfos & "BeginSong{" & vbNewLine
songInfos = songInfos & "Song Name2" & vbNewLine
songInfos = songInfos & "Artist2" & vbNewLine
songInfos = songInfos & "Genre2" & vbNewLine
songInfos = songInfos & "}EndSong"
firstArray = Split(songInfos, "BeginSong{")
songCounter = 0
ReDim songInfoArray(2, 0)
For i = 1 To UBound(firstArray) Step 1
secondArray = Split(firstArray(i), "}EndSong")
thirdArray = Split(secondArray(0), vbNewLine)
songInfoArray(0, songCounter) = thirdArray(1)
songInfoArray(1, songCounter) = thirdArray(2)
songInfoArray(2, songCounter) = thirdArray(3)
songCounter = songCounter + 1
If i < UBound(firstArray) Then
ReDim Preserve songInfoArray(2, songCounter)
End If
Next i
最后一行之后的手表。请注意songInfoArray的结构,它是ReDim
med