Split(String) 导航 - “转到下一个子字符串”
我绝不是一个经验丰富的程序员。我的这项任务很可能是一次性的,所以不要因为给我答案而不是为我指明正确的方向而感到难过: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要做这样的事情。您想要做的是将索引向两个方向移动。您需要迭代子字符串,直到看到
#
,问题是一旦到达那里,您的For
语句就会将其移过该记录。我使用您的文件创建了一个示例,该示例似乎可以使用基本的For
语句进行工作。看看它是否适合你。它的输出看起来像:
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 yourFor
statement will move it past that record. I have created an example using your file that seems to work using a basicFor
statement. See if it works for you.It's output looks like: