当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 在逗号上拆分字符串并打印结果

来源:互联网 收集:自由互联 发布时间:2021-06-24
我使用以下代码拆分字符串并检索它们: 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`
网友评论