2017-07-16 12 views
0

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 
+0

文字列をequals not ==を使用して比較する必要がありますが、現在の出力は何ですか? –

+0

@Killer Death: 'Test1.txt'には末尾のスペースが含まれているようです。そのような問題を解決するために 'Trim()'をコードに追加しました(私の編集を参照) –

+0

ドミトリー。コードは機能しています。どうもありがとうございました。私は辞書についてもっと学びます。神のご加護を。 – thompogi

答えて

0

私はキーのコレクションを構築するために、辞書を使用することをお勧め/値のペア:

{ "T2", "James"} 
    { "T3", "Cindy"} 
    { "S2", "John"} 
    { "S3", "Martha"} 

あなたはこの方法でそれを実装することができます。

あなたは簡単に対応する値を見つけることができ辞書持つ
using System.Linq; 
using System.IO; 

... 

Dictionary<string, string> CodeToName = File 
    .ReadLines("Test2.txt") 
    .Select(line => line.Split(',')) 
    .GroupBy(items => items[0].Trim()) 
    .ToDictionary(chunk => chunk.Key, 
       chunk => string.Join("; ", chunk.Select(item => item[1].Trim()))); 

:C#7.0以降あなたは

に、後を簡素化することができた場合には

string name = null; 

File.WriteAllLines("Test3.txt", File 
    .ReadLines("Test1.txt") 
    .Select(line => CodeToName.TryGetValue(line.Trim(), out name) 
    ? name 
    : line)); 

File.WriteAllLines("Test3.txt", File 
    .ReadLines("Test1.txt") 
    .Select(line => CodeToName.TryGetValue(line.Trim(), out var name) // out var syntax 
    ? name 
    : line)); 
+0

Hi Dmitryに置き換えることができます。返信いただきありがとうございます。私はコードを使用しましたが、私は現在の出力と同じ結果を得ています。 T1、T2、T3、S1、S2、S3 – thompogi

0

var file1 = File.ReadAllLines(@"D:\Test1.txt"); 
var file2 = File.ReadAllLines(@"D:\Test2.txt"); 

var result = file1.Select(l1 => 
    file2.FirstOrDefault(l2 => l2.StartsWith($"{l1},"))?.Substring(l1.Length + 1) ?? l1); 
File.WriteAllLines(@"D:\Test3.txt", result); 
+0

こんにちはAleks。返信いただきありがとうございます。コードを試しましたが、予期しない文字 '$'が表示されています。 – thompogi

+0

@ThomasArriola文字列補間、C#6の機能です。 '$" {l1}、 ''を 'l1 +"、 "' –