Vb.net 拆分(字符串)导航-";“转到下一个子字符串”;
我绝对不是一个有经验的程序员。我的这项任务很可能是一次性的,所以不要为给我答案而不是为我指出正确的方向而感到难过:P 我已经找了很久了,就是找不到我要找的东西 我只需要能够移动到字符串的下一个子字符串。 在本例中,“转到下一个子字符串”等同于“转到下一行”。 我只需要被告知一个命令,我就可以上路了,但我找不到任何线索 下面是安装了magic命令的代码片段:Vb.net 拆分(字符串)导航-";“转到下一个子字符串”;,vb.net,string,navigation,split,Vb.net,String,Navigation,Split,我绝对不是一个有经验的程序员。我的这项任务很可能是一次性的,所以不要为给我答案而不是为我指出正确的方向而感到难过:P 我已经找了很久了,就是找不到我要找的东西 我只需要能够移动到字符串的下一个子字符串。 在本例中,“转到下一个子字符串”等同于“转到下一行”。 我只需要被告知一个命令,我就可以上路了,但我找不到任何线索 下面是安装了magic命令的代码片段: Dim count As Integer Dim line As String Dim lines As String() = My.Com
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
如果我太离谱了,我向你道歉。我只是把从各种教程中找到的零碎东西放在一起
我已经成功地获得了谷歌单独使用时所需的所有其他功能,但最后一项任务让我陷入困境
谢谢 我认为你需要这样做。您试图做的是将索引向两个方向移动。您需要迭代子字符串,直到看到一个
#
问题是,一旦到达那里,您的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
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())