-2

I have three arays:

string[] comet1 ={comenta1,comenta2,coment3,......This way for all the selected elements};
string[] paramet1 = { long1,long2,lon2,......};
string[] parametr2={ alt1,alt2,alt2,......};

TextWriter tw = new StreamWriter("C:/archivo.txt")

// añadir linea de texto al archivo de texto
for (int i = 0; i < comet1.Length; i++)
{
    tw.WriteLine(comet1[i]);
}

for (int a = 0; a < paramet1.Length; a++ )
{
    tw.WriteLine(paramet1[a]);
}

Using that code the data is added to the txt file as following:

comenta1
comenta2

long1
long2

.....

What I want to do is :

comenta1/long1
comenta2/long2

......

I tried:

tw.WriteLine(comet1[i] + "\\" + paramet1[a]);

But that only adds the first line two times!

KURRY
  • 1
  • 1
  • 2
    Shouldn't it be `tw.WriteLine(comet1[i] + "\\" + paramet1[i]);`? You'll only need one loop,assuming all the `string[]` has the same length – Pikoh Jan 11 '17 at 12:30
  • yes¡¡¡¡¡¡ oh my good....thank pikoh, I think that it was too much tired to see it. – KURRY Jan 11 '17 at 12:43
  • @KURRY, 1/. What are you trying to do ? 2/. What if you have uneven source data? 3/. You start with i have tree array.. but only two are used, i must have miss something. 4/. What if there is a \ in your string ? – Drag and Drop Jan 11 '17 at 12:43
  • You're welcome @KURRY . Anyway, have a look at InBetween answer, it's a very clean way of achieving the same – Pikoh Jan 11 '17 at 12:45

2 Answers2

1

You can do this with a one liner using the Enumerable.Zip extension method found in System.Linq and String.Join:

tw.WriteLine(string.Join(Environment.NewLine, comet1.Zip(paramet1, (f, s) => $"{f}\\{s}")));

Read the documentation of both methods in the provided links to understand exactly what they do.

InBetween
  • 32,319
  • 3
  • 50
  • 90
0

Try this:

for (int i = 0; i < comet1.Length; i++)
{
    tw.WriteLine(string.Concat(comet1[i], "/", paramet1[i]));
}