简体   繁体   English

在Visual Basic中串联数组的元素

[英]Concatenating elements of an array in Visual Basic

Hey ive run into a troubling problem basically I have an array Dim array() as String = {"Team1, "Team2", "Team3", "Team4", "Team5" } 嘿,我遇到了一个麻烦的问题,基本上我有一个数组Dim array() as String = {"Team1, "Team2", "Team3", "Team4", "Team5" }

My goal is to concatenate the strings in even pairs with the string "vs" AND if there is an odd number of pairs to concatenate the odd element in the array with the string "Bye" So im trying to get my output to look like this Team1 vs Team2, Team3 vs Team4, Team5 vs Bye 我的目标是将偶数对中的字符串与字符串“ vs”连接起来并且如果存在奇数对,则将字符串中的奇数元素与字符串“ Bye”连接起来, 所以我想让我的输出看起来像这样Team1 vs Team2, Team3 vs Team4, Team5 vs Bye

I know that I need to use loops however im getting confused on how i can get the unknown number of elements in the original array into even pairs of 2 so that i can concatenate the strings! 我知道我需要使用循环,但是我对如何将原始数组中未知数量的元素转换为偶数对2感到困惑,以便我可以串联字符串! Any help would be much appreciated! 任何帮助将非常感激!

I would avoid loops and use LINQ. 我会避免循环并使用LINQ。 Here's what I would do: 这就是我要做的:

Dim array As String() = { "Team1", "Team2", "Team3", "Team4", "Team5" }

Dim working = array
If working.Length Mod 2 = 1 Then
    working = working.Concat({ "Bye" }).ToArray()
End If

Dim output = _
    working.Where(Function (x, n) n Mod 2 = 0) _
        .Zip(working.Where(Function (x, n) n Mod 2 = 1), _
            Function (x1, x2) String.Format("{0} vs {1}", x1, x2)) _
    .ToArray()

This gives: 这给出:

输出

Another alternative way of doing this with LINQ is this: 使用LINQ的另一种替代方法是:

Dim output = _
    working _
        .Select(Function (x, n) New With { .Team = x, .Group = n \ 2 }) _
        .GroupBy(Function (x) x.Group, Function (x) x.Team) _
        .Select(Function (xs) String.Join(" vs ", xs)) _
        .ToArray()

With 8 teams: 有8个团队:

Team1 vs Team2 
Team3 vs Team4 
Team5 vs Team6 
Team7 vs Team8 

With 3 teams: 有3个团队:

Team1 vs Team2 
Team3 vs Bye 

With 10,001 teams: 拥有10001个团队:

Team1 vs Team2 
Team3 vs Team4 
Team5 vs Team6 
...
Team9997 vs Team9998 
Team9999 vs Team10000 
Team10001 vs Bye 

If you need it as a string, rather than an array, then just do this: 如果您需要将其作为字符串而不是数组,则只需执行以下操作:

Dim text = String.Join(", ", output)

This gives you this kind of thing: 这给了你这种东西:

Team1 vs Team2, Team3 vs Team4, Team5 vs Team6, Team7 vs Team8, Team9 vs Team10, Team11 vs Team12, Team13 vs Team14, Team15 vs Team16, Team17 vs Bye Team1 vs Team2,Team3 vs Team4,Team5 vs Team6,Team7 vs Team8,Team9 vs Team10,Team11 vs Team12,Team13 vs Team14,Team15 vs Team16,Team17 vs Bye

You can increment a For loop by 2 by adding a Step parameter. 您可以通过添加Step参数使For循环增加2。 But instead of loop to Count - 1, you should loop to one-half of that. 但是,而不是循环到Count-1,您应该循环到一半。 This depends somewhat on the odd/evenness of the number of teams. 这在某种程度上取决于球队数量的奇/偶。 When concatenating, use the loop parameter and the parameter plus 1 to access the array elements. 连接时,请使用loop参数和参数加1来访问数组元素。 Example: 例:

dim game as string = teams(i) & teams(i + 1)

You can make a for loop and use a step of 2. Use a StringBuilder in order to assemble the final string. 您可以进行for循环并使用步骤2。使用StringBuilder组装最终字符串。

Dim array() as String = { "Team1", "Team2", "Team3", "Team4", "Team5" }
Dim sb As New StringBuilder() 
For i = 0 To array.Length - 1 Step 2
    Dim s1 As String = array(i) 
    Dim s2 As String = If(i + 1 < array.Length, array(i + 1), "Bye")
    sb.Append(s1).Append(" vs ").Append(s2).Append(", ")
Next
If sb.Length > = 2 Then
    sb.Length -= 2
End If
Dim result = sb.ToString() ' ==> "Team1 vs Team2, Team3 vs Team4, Team5 vs Bye"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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