我有兩個文本文件。等於兩個文本文件的字符串數組
第1文本文件(個Test1.txt)具有含量象下面這樣:
T1
T2
T3
S1
S2
S3
第二文本文件(的test2.txt)具有含量象下面這樣:
T2,James
T3,Cindy
S2,John
S3,Martha
所需的輸出(Test3的.txt)如下:
T1
James
Cindy
S1
John
Martha
我試過下面的代碼,但似乎沒有考慮第二個文本文件。需要你的幫助人員來糾正我的代碼。先進的非常感謝。
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