2つのテキストファイルがあります。2つのテキストファイルの文字列配列の比較
まずテキストファイル(test1.txtという)は、以下のような内容を持っています
T1
T2
T3
S1
S2
S3
2番目のテキストファイル(test2.txtという名前)は、以下のような内容を持っています
T2,James
T3,Cindy
S2,John
S3,Martha
所望の出力(Test3は.txt)は次のとおりです:
T1
James
Cindy
S1
John
Martha
私は以下のコードを試しましたが、2番目のテキストファイルが考慮されていないようです。あなたの助けを借りて私のコードを修正する必要があります。非常に進んでいただきありがとうございます。
string line;
string DatabaseFullPath = @"D:\Test1.txt";
string line2;
string DatabaseFullPath2 = @"D:\Test2.txt";
//write to new text file
using (StreamWriter writetext = new StreamWriter(@"D:\Test3.txt"))
//read second text file
using (var file2 = new StreamReader(DatabaseFullPath2))
{
line2 = file2.ReadLine();
var ProjectInfo2 = line2.Split(',');
//read first text file
using (var file = new StreamReader(DatabaseFullPath))
{
//loop on all lines of first text file
while ((line = file.ReadLine()) != null)
{
//compare lines with all the first column of second text file
if (line == ProjectInfo2[0])
{
//put ProjectInfo2[1] on label 1. label 1 as a container
label1.Text = ProjectInfo2[1];
}
else
{
//put line on label 1. label 1 as a container
label1.Text = line.Trim();
}
//write all values of label1.Text
writetext.WriteLine(label1.Text.Trim());
}
}
}
電流出力:あなたは、このアプローチを使用することができ、小さなファイルの場合は
T1
T2
T3
S1
S2
S3
文字列をequals not ==を使用して比較する必要がありますが、現在の出力は何ですか? –
@Killer Death: 'Test1.txt'には末尾のスペースが含まれているようです。そのような問題を解決するために 'Trim()'をコードに追加しました(私の編集を参照) –
ドミトリー。コードは機能しています。どうもありがとうございました。私は辞書についてもっと学びます。神のご加護を。 – thompogi