0

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: e.g. Name1 100 200 300

...etc..

3
  • 2
    Use String.Split? Commented Nov 7, 2012 at 2:07
  • Yes, but it splits the first comma, so how do I make it split each one? I know I have to use a loop, but what about the next comma? If someone could post an example, that would help! Commented Nov 7, 2012 at 2:10
  • String.Split splits the whole line, not just the first word. Commented Nov 7, 2012 at 2:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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

11 Comments

Thank you so much! It says Option Strict On disallows late binding. Any idea? *I am not turning off Option Strict on.
on which line is that, I didn't get that error even after turning option strict on?
I have added .ToCharArray() to the Split. That may cause it
Just a guess: maybe you need to CType(lstInput.Items(s), String)?
BTW, you can use ","c instead of ",".ToCharArray().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.