[英]Split an array in Visual Basic
我在lstInput(一個列表框)中有一個帶分數的名稱列表,看起來像這樣:
Name1,100,200,300
Name2,100,200,300
Name3,100,200,300
...等等...
我需要將數組拆分為一個字符串,並打印此人的名字和以逗號分隔的分數的結果。
我到目前為止有以下內容:
For s As Integer = 0 To lstInput.Items.Count - 1
lstOutput.Items.Add(lstInput.Items(s))
Next
現在,將顯示整個列表,但是我需要將列表拆分為字符串,以便它們自己顯示:例如Name1 100 200 300
...等等..
我可能會發瘋,但是我認為OP想要這樣的事情:
For s As Integer = 0 To lstInput.Items.Count - 1
lstOutput.Items.Add(String.Join(" ", CType(lstInput.Items(s), String).Split(",")))
Next
該代碼的用途未知,但最終會刪除逗號,因此此Name1,100,200,300
變為此Name1,100,200,300
Name1 100 200 300
(僅在問題后面)。 猜猜我本來可以做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.