sharpie_0815
sharpie_0815

Reputation: 45

C# Join Strings of two arrays

I have two string arrays

array1 = { "test", "test", "test" }
array2 = { "completed", "completed", "completed" }

And I want to join the strings in the two arrays (they are always the same size) -> so I want to have one array which contains

array = { "test completed", "test completed", "test completed" }

Everything I found was only joining the arrays so I have a 6 items in array. Is it possible to do this without looping through the whole array (i.e. with LINQ or something like that) ?

Upvotes: 3

Views: 1071

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

You can do it with LINQ using Zip:

var res = array1.Zip(array2, (a, b) => $"{a} {b}").ToArray();

Note: If you do not have the latest compiler, use a+" "+b instead of $"{a} {b}".

Upvotes: 8

Giorgi
Giorgi

Reputation: 30873

You can do it with Enumerable.Zip method like this:

var joined = array1.Zip(array2, (first, second) => first + " " + second);

Upvotes: 8

Related Questions