user732294
user732294

Reputation:

concatinating int[] to same size string[]

i have:

int[] numbers = { 1, 2, 3};
string[] words = { "one", "two", "three" };

and need the output to be

1=one
2=two
3=three

thanks

Upvotes: 2

Views: 134

Answers (3)

Marino Šimić
Marino Šimić

Reputation: 7342

I see that everybody is jumping to Linq or IEnumerable extensions when you don't grasp the basics. It will be like putting a child to college so I suggest you first learn using loops, like the for loop.

for (int i = 0; i < numbers.Length; i++) {
  Console.WriteLine(String.Format("{0}-{1}", numbers[i], words[i]));
}

And Math class basics

int total = Math.Min(numbers.Length, word.Length);
for (int i = 0; i < total; i++) {

Upvotes: 6

k.m
k.m

Reputation: 31464

LINQ example:

var query = numbers.Select((n, i) => string.Format("{0}={1}", n, words[i]));

Edit:

In .NET 4.0 you can use Zip (as posted by mBotros) - there's even almost same example as what you're asking for on Zip MSDN doc page.

Upvotes: 3

Maged Samaan
Maged Samaan

Reputation: 1752

if they are the same size you can use

int[] numbers = { 1, 2, 3};
string[] words = { "one", "two", "three" };

var list = numbers.Zip (words, (n, w) => n + "=" + w);

but note if they differ in size the non match items will be ignored

Upvotes: 7

Related Questions