2013-03-18 33 views
0

我有音樂列表,看起來像這樣的文本文件:獲取符之間的文本字符串中的

BeginSong{ 
Song Name 
Artist 
Genre 
}EndSong 

這方面有多個實例。

我想獲取BeginSong{}EndSong之間的文本,並將歌曲信息 放入字符串數組中。然後,我想將每個實例添加到列表框中作爲藝術家 - 歌曲名稱 (但我確信我可以弄清楚這一點)。我希望這是一個明確的描述。

+5

爲什麼當你使用的是VS2010和VB.net陳述VB6標籤要求? – 2013-03-18 02:17:59

+0

爲什麼不使用現有的標準,如CSV或XML/JSON? – Deanna 2013-03-18 21:30:31

回答

1

使用的FileStream

ReadLine()功能,因爲你已經知道的信息的順序,你應該能夠循環中的所有文件行,並將它們存儲在他們相應的屬性。

僞:

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 
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 
0

有來自Neolisk一個很好的答案,它使用常用表達。但是,既然您還包含了VB.NET標籤,我將在VB6解決方案中進行一些嘗試。

您可以使用字符串分割功能,和分裂的「目的」,即"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配有

enter image description here

相關問題