vb.net 在 Visual Basic 中拆分数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13262256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:03:01  来源:igfitidea点击:

Split an array in Visual Basic

vb.net

提问by AAA

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

我在 lstInput(一个列表框)中有一个带有分数的名字列表,看起来像这样:

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

现在,这会显示整个列表,但我需要将列表拆分为字符串,以便它们自己显示:例如 Name1 100 200 300

...etc..

...等等..

回答by Neolisk

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

我可能会发疯,但我认为 OP 想要这样的东西:

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,300becomes this Name1 100 200 300(just following the question). Guess I could have done String.Replaceinstead, but it's not as cool.

这段代码的目的是未知的,但它最终删除了逗号,所以这Name1,100,200,300变成了这个Name1 100 200 300(只是在问题之后)。我猜我本来可以这样做的String.Replace,但它没有那么酷。

回答by codingbiz

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