简体   繁体   中英

Split an array in Visual Basic

I have a list of names with scores in lstInput (a listbox) that looks something like this:

Name1,100,200,300
Name2,100,200,300
Name3,100,200,300

...etc...

I need to split the array into a string and print the results of the person's name and the scores that are separated by a comma.

What I have so far is the following:

For s As Integer = 0 To lstInput.Items.Count - 1
    lstOutput.Items.Add(lstInput.Items(s))
Next

Now, that displays the entire list, but I need to split the list into strings so that they display on their own: eg Name1 100 200 300

...etc..

I may be going crazy, but I think the OP wants something like this:

For s As Integer = 0 To lstInput.Items.Count - 1
  lstOutput.Items.Add(String.Join(" ", CType(lstInput.Items(s), String).Split(",")))
Next

Purpose of this code is unknown but it ultimately removes commas, so this Name1,100,200,300 becomes this Name1 100 200 300 (just following the question). Guess I could have done String.Replace instead, but it's not as cool.

For s As Integer = 0 To lstInput.Items.Count - 1
    dim items As String() = lstInput.Items(s).Split(",".ToCharArray()) 'splits into array of 4 elements

    dim name As String = items(0) 'first element is name
    dim score1 As String = items(1) 'second element is first score

    -- now do the rest yourself

    -- listOutput.Items.Add( concatenate name and the scores here)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM