我正在阅读两个文件并比较它们,我意识到我需要考虑的一件事是删除间距,因为它会导致差异,我不希望间距成为差异的一个方面,所以我想删除它.
这是我到目前为止所拥有的:
Dictionary<string, int> Comparer = new Dictionary<string, int>();
string line;
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
while (sr.Peek() >= 0 )
{
line = sr.ReadLine();
if (Comparer.ContainsKey(line))
Comparer[line]++;
else
Comparer[line] = 1;
}
}
using (StreamReader sr = new StreamReader(openFileDialog2.FileName))
{
while (sr.Peek() >= 0)
{
line = sr.ReadLine();
if (Comparer.ContainsKey(line))
Comparer[line]--;
else
Comparer[line] = -1;
}
}
int mismatches = 0;
var firstFileChanges = new List<string>();
var secondFileChanges = new List<string>();
System.Text.StringBuilder theStringBuilder = new System.Text.StringBuilder();
foreach (KeyValuePair<string, int> kvp in Comparer)
{
if (kvp.Value != 0)
{
mismatches++;
string InWhich = kvp.Value > 0 ? openFileDialog1.FileName : openFileDialog2.FileName;
if (InWhich == openFileDialog1.FileName)
{
firstFileChanges.Add(kvp.Key);
}
else
{
secondFileChanges.Add(kvp.Key);
}
}
}
if (firstFileChanges.Count > 0)
{
theStringBuilder.Append("ADDED IN " + openFileDialog1.SafeFileName+": \n");
int counter1 = 0;
foreach (string row in firstFileChanges)
{
if (counter1 > 0)
{
theStringBuilder.Append("\n ");
}
theStringBuilder.Append(row);
counter1 += 1;
}
theStringBuilder.AppendLine();
}
if (secondFileChanges.Count > 0)
{
theStringBuilder.Append("\nDELETED FROM "+openFileDialog2.SafeFileName+": \n");
int counter2 = 0;
foreach (string row in secondFileChanges)
{
if (counter2 > 0)
{
theStringBuilder.Append("\n ");
}
theStringBuilder.Append(row);
counter2 += 1;
}
}
示例输入文件:名称 (spaaaaaaace) 标题 (spaaaaaaace) 状态
我希望它是: 姓名 职务 状态