2012-02-22 66 views
-1

我絕不是一個有經驗的程序員。我的這個任務很可能是一次性的,所以不要指責我在正確的方向上給我答案:P拆分(字符串)導航 - 「轉到下一個子字符串」

我已經搜索了,只要我能承受,只是可以我找不到我想要的。

我只需要能夠移動到字符串的下一個子字符串。 在這種情況下,「轉到下一個子字符串」等同於「轉到下一行」。 我只需要告訴我一個命令和我會在路上,但我找不到它的任何痕跡。

這裏的代碼與魔法命令安裝一個片段:

Dim count As Integer 
Dim line As String 
Dim lines As String() = My.Computer.FileSystem.ReadAllText("C:\test.txt").Split(New Char() {vbCrLf}) 

For Each line In Lines 
    If line.Contains("#") 
     count = 0 
     **GO TO NEXT LINE** 
     Do Until line.Contains("#") 
      count = count + 1 
      **GO TO NEXT LINE** 
     Loop 
     Console.WriteLine(line & ", " & count) 
    End If 
Next 

除非我失去了一些東西,我應該可以使用格式如下文字:

#VERSE1 
Lyrics lyrics 
Lyrics lyrics 
#CHORUS1 
Lyrics lyrics 
Lyrics lyrics 
Lyrics lyrics 
Lyrics lyrics 
#VERSE2 
Lyrics lyrics 
Lyrics lyrics 
Lyrics lyrics 
#CHORUS2 
Lyrics lyrics 
Lyrics lyrics 
Lyrics lyrics 
Lyrics lyrics 
Lyrics lyrics 
#END 

和接收結果如下:

#VERSE1, 2 
#CHORUS1, 4 
#VERSE2, 3 
#CHORUS2, 5 
#END, 0 

我很抱歉,如果我瘋狂離譜。我只是把我從各種教程中找到的點點滴滴放在一起。

我已經設法得到我需要單獨使用谷歌搜索的所有其他功能,但這最後一項任務讓我陷入困境。

謝謝!

回答

1

我認爲你需要做這樣的事情。你試圖做的是將你的索引向兩個方向移動。您需要遍歷子字符串,直到您看到#問題出現後,您的For語句會將其移過該記錄。我已經創建了一個使用您的文件的例子,似乎使用基本的For語句工作。看看它是否適合你。

Sub Main() 
    Dim count As Integer 
    Dim x As Integer 
    Dim line As String 
    Dim lines As String() = My.Computer.FileSystem.ReadAllText("C:\test.txt").Split(New Char() {vbCrLf}) 

    For x = 0 To lines.Length - 1 
     If lines(x).Contains("#") Then 
      line = lines(x) 
      count = 0 
      x += 1 
      If x < lines.Length - 1 Then 
       Do Until lines(x).Contains("#") 
        count += 1     'Increment Counter 
        x += 1      'Point to next Line 
       Loop 
      End If 
      Console.WriteLine(line & ", " & count) 
      x -= 1        ' Set x back to the line before the # so the for statement will find correct line. 
     End If 
    Next 

    Console.ReadLine() 
End Sub 

它的輸出如下:

#VERSE1 , 2 

#CHORUS1 , 4 

#VERSE2 , 3 

#CHORUS2 , 5 

#END , 0 
+0

也就是說_absolutely_完美。謝謝x1,000,000,000! – aener 2012-02-22 01:02:59

+0

很高興能有所幫助 – 2012-02-22 01:05:52

0
Dim count As Integer = 0 
    Dim line As String = String.Empty 
    Dim strSongSegment As String = String.Empty 
    Dim lines As String() = My.Computer.FileSystem.ReadAllText("C:\test.txt").Split(New Char() {vbCrLf}) 

    For Each line In lines 

     'Is this a new segment of the song? 
     If line.Contains("#") Then 

      'Make sure its not the first segment. 
      '(Note that .Length is a more modern approach.) 
      If Len(strSongSegment) > 0 Then 

       Console.WriteLine(strSongSegment & ", " & count.ToString()) 

      End If 

      'Keep track of this until we have the finaly tally for this segment. 
      strSongSegment = line 

      'Look down a couple lines of code to see why this is -1. 
      count = -1 

     End If 

     'Increment the cursor. 
     count = count + 1 

    Next 

    'Finally display the total for the last segment. 
    Console.WriteLine(line & ", " & count.ToString())