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

发布于 2025-01-07 11:09:21 字数 1166 浏览 0 评论 0原文

我绝不是一个经验丰富的程序员。我的这项任务很可能是一次性的,所以不要因为给我答案而不是为我指明正确的方向而感到难过: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

如果我完全偏离目标,我深表歉意。我只是将我从各种教程中找到的零碎内容放在一起。

我已经成功地通过谷歌搜索获得了我需要的所有其他功能,但是最后一项任务让我陷入困境。

谢谢!

I'm by no means an experienced programmer. This task I have is very likely a one-off, so don't feel bad for giving me answers instead of pointing me in the right direction :P

I've searched as long as I can bear, and just can't find what I'm after.

I just need to be able to move to the next substring of a string.
In this instance, "go to next substring" equates to "go to next line".
I just need to be told that one command and I'll be on my way, but I can't find any trace of it.

Here's a snippet of code with the magic command installed:

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

Unless I'm missing something, I should be able to use text formatted like this:

#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

And receive the result:

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

I apologize if I am wildly off the mark. I'm just putting together bits and pieces I've found from various tutorials.

I've managed to get all the other functions I need working with Googling alone, but this last task has me stuck.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2025-01-14 11:09:21

我认为你需要做这样的事情。您想要做的是将索引向两个方向移动。您需要迭代子字符串,直到看到 # ,问题是一旦到达那里,您的 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

I think you need to do something like this. What you are trying to do is move your index in two directions. You need to iterate over the substrings till you see a # the problem being once you get there your For statement will move it past that record. I have created an example using your file that seems to work using a basic For statement. See if it works for you.

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

It's output looks like:

 #VERSE1 , 2

 #CHORUS1 , 4

 #VERSE2 , 3

 #CHORUS2 , 5

 #END , 0
沧笙踏歌 2025-01-14 11:09:21
    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())
    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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文