[英]Split an array in Visual Basic
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: eg Name1 100 200 300 现在,将显示整个列表,但是我需要将列表拆分为字符串,以便它们自己显示:例如Name1 100 200 300
...etc.. ...等等..
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,300
becomes this Name1 100 200 300
(just following the question). 该代码的用途未知,但最终会删除逗号,因此此Name1,100,200,300
变为此Name1,100,200,300
Name1 100 200 300
(仅在问题后面)。 Guess I could have done String.Replace
instead, but it's not as cool. 猜猜我本来可以做String.Replace
,但它并不那么酷。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.