在逗号上拆分字符串并打印结果 [英] Splitting a string on comma and printing the results

查看:26
本文介绍了在逗号上拆分字符串并打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码拆分字符串并检索它们:

I am using the following code to split a string and retrieve them:

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)中的第一个字符.

But the message box shows only the first character in each splitted string (a,b,d,g,k).

我只想显示第一个单词,我做错了什么?

I want to show only the first word, what am I doing wrong?

推荐答案

从您的问题中不清楚,但如果您只想要字符串数组中的 第一个单词,则无需循环结束了

It is not clear from your question, but if you want only the first word in your array of strings then no need to loop over it

 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`

这篇关于在逗号上拆分字符串并打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆