在VB.net(Visual Studio 2015)中,如何在逗号分隔列表中获取第n个字符串(或数字)?
说我有一个以逗号分隔的数字列表,如此:
13,1,6,7,2,12,9,3,5,11,4,8,10
答案 0 :(得分:1)
如果您正在寻找更基本的替代方法,您可以尝试:
Module Module1
Sub Main()
Dim a As String = "13,1,6,7,2,12,9,3,5,11,4,8,10"
Dim counter As Integer = 5 'the number you want (in this case, 5th one)
Dim movingcounter As Integer = 0 'how many times we have moved
Dim startofnumber, endofnumber, i As Integer
Dim numberthatIwant As String
Do Until movingcounter = counter
startofnumber = InStr(i + 1, a, ",")
i = startofnumber
movingcounter = movingcounter + 1
Loop
endofnumber = InStr(startofnumber + 1, a, ",")
numberthatIwant = (Mid(a, startofnumber + 1, endofnumber - startofnumber - 1))
Console.WriteLine("The number that I want: " + numberthatIwant)
Console.ReadLine()
End Sub
End Module
编辑:如果你想在一个更大的程序中使用它,你可以把它变成一个过程或函数,但是这个在控制台模式下运行的代码会输出12。
答案 1 :(得分:0)
Plutonix提供的解决方案是对我的问题的评论很简单,正是我所寻找的,例如:
result = csv.Split(“,”c)(5)
result = WholeString.Split(“,”c)(IncrementedVariable)