我使用以下代码拆分字符串并检索它们: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim s As String = "a,bc,def,ghij,klmno" Dim parts As String() = s.Split(New Char() {","c}) Dim part As
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim s As String = "a,bc,def,ghij,klmno" Dim parts As String() = s.Split(New Char() {","c}) Dim part As String For Each part In parts MsgBox(part(0)) Next End Sub
但是消息框仅显示每个分割字符串中的第一个字符(a,b,d,g,k).
我想只显示第一个字,我做错了什么?
从你的问题中不清楚,但如果你只想要你的字符串数组中的第一个单词,那么就不需要循环它了Dim firstWord = parts(0) Console.WriteLine(firstWord) ' Should print `a` from your text sample ' or simply Console.WriteLine(parts(0)) ' and the second word is Console.WriteLine(parts(1)) ' prints `bc`