将两个数组的元素成对组合
Combine elements of two arrays in pairs
我有三个数组:
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]);
}
使用该代码将数据添加到 txt
文件,如下所示:
comenta1
comenta2
long1
long2
.....
我想做的是:
comenta1/long1
comenta2/long2
......
我试过:
tw.WriteLine(comet1[i] + "\" + paramet1[a]);
但这只会将第一行添加两次!
试试这个:
for (int i = 0; i < comet1.Length; i++)
{
tw.WriteLine(string.Concat(comet1[i], "/", paramet1[i]));
}
您可以使用 Enumerable.Zip
extension method found in System.Linq
and String.Join
:
用一个衬垫来做到这一点
tw.WriteLine(string.Join(Environment.NewLine, comet1.Zip(paramet1, (f, s) => $"{f}\{s}")));
阅读提供的链接中这两种方法的文档,以准确了解它们的作用。
我有三个数组:
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]);
}
使用该代码将数据添加到 txt
文件,如下所示:
comenta1
comenta2
long1
long2
.....
我想做的是:
comenta1/long1
comenta2/long2
......
我试过:
tw.WriteLine(comet1[i] + "\" + paramet1[a]);
但这只会将第一行添加两次!
试试这个:
for (int i = 0; i < comet1.Length; i++)
{
tw.WriteLine(string.Concat(comet1[i], "/", paramet1[i]));
}
您可以使用 Enumerable.Zip
extension method found in System.Linq
and String.Join
:
tw.WriteLine(string.Join(Environment.NewLine, comet1.Zip(paramet1, (f, s) => $"{f}\{s}")));
阅读提供的链接中这两种方法的文档,以准确了解它们的作用。