Concat String Array C#

  • A+
Category:Languages

I have two string arrays, I want them to become one with differente values like this:

string[] array1 = { "Jhon", "Robert", "Elder" }; string[] array2 = { "Elena", "Margareth", "Melody" }; 

I want an output like this:

{ "Jhon and Elena", "Robert and Margareth", "Elder and Melody" }; 

I've used string.Join, but it works only for one string array.

 


It sounds like you want Zip from LINQ:

var result = array1.Zip(array2, (left, right) => $"{left} and {right}").ToArray(); 

Zip takes two sequences, and applies the given delegate to each pair of elements in turn. (So the first element from each sequence, then the second element of each sequence etc.)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: