拆分(字符串)导航 - “转到下一个子字符串”

时间:2012-02-22 00:16:05

标签: vb.net string navigation split

我绝不是一位经验丰富的程序员。我的这项任务很可能是一次性的,所以不要因为给我答案而不是指向正确的方向而感到难过:P

只要我能忍受,我就一直在寻找,而且找不到我想要的东西。

我只需要能够移动到字符串的下一个子串。 在这种情况下,“转到下一个子串”等同于“转到下一行”。 我只需要被告知一个命令,我将在路上,但我找不到任何痕迹。

以下是安装了magic命令的代码片段:

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

如果我疯狂地离开,我道歉。我只是把我从各种教程中找到的点点滴滴放在一起。

我已经设法获得了我需要单独使用谷歌搜索所需的所有其他功能,但最后一项任务让我陷入困境。

谢谢!

2 个答案:

答案 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

答案 1 :(得分: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())
相关问题