简体   繁体   中英

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" }

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

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! Any help would be much appreciated!

I would avoid loops and use 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:

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:

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

With 3 teams:

Team1 vs Team2 
Team3 vs Bye 

With 10,001 teams:

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

You can increment a For loop by 2 by adding a Step parameter. But instead of loop to Count - 1, you should loop to one-half of that. 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. 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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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