string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
temp = temp.ToList().Distinct().ToArray();
File.WriteAllLines(@"C:\1.txt", temp);例如数据是这样:
-------------------------------------------
00 2012.05 xxx 12345
00 2012.05 xxx 12300
00 2012.05 xxx 12388
00 2012.06 xxx 12345
我只想判断12345这一列数据是否重复,请问代码该怎么变化呢?
temp = temp.ToList().Distinct().ToArray();
File.WriteAllLines(@"C:\1.txt", temp);例如数据是这样:
-------------------------------------------
00 2012.05 xxx 12345
00 2012.05 xxx 12300
00 2012.05 xxx 12388
00 2012.06 xxx 12345
我只想判断12345这一列数据是否重复,请问代码该怎么变化呢?
select new { x, y = x.Split(' ')[3] })
group z by z.y into g
select z.First().x).ToArray();
{
public bool Equals(string x, string y)
{
return x.Split(' ')[3] == y.Split(' ')[3];
} public int GetHashCode(string x)
{
return x.Split(' ')[3];
}
}string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
temp = temp.ToList().Distinct(new MyComparer()).ToArray();但是很明显,这种写法多定义一个类。所以我不喜欢。
//定义四个数组
ArrayList number = new ArrayList(); protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string Path = Server.MapPath("info.txt");
getStr(Path); for (int i = 0; i < number.Count; i++)
{
Response.Write(number[i].ToString()+" | ");
//这里将所有的最后一列数据取出来了,然后判断是否有重复应该很简单了
}
}
} //读取txt行中的每一个数据
public void getStr(string Path)
{
string strLine = "";
int i = 0;
try
{
StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));
while ((strLine=sr.ReadLine()) != null)
{
string[] val = strLine.Split(' ');
//将数据保存在ArrayList集合中
number.Add(val[3]);
Response.Write("当前第"+i+"行:<br/>");
foreach (string s in val)
{
Response.Write(s + "<br>");
}
i++;
Response.Write("<hr>");
}
sr.Dispose();
sr.Close();
}
catch
{
}
}
List<string> liststr = new List<string>();
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (string tempeach in temp)
{
liststr.Add(tempeach.Split(' ').ToList().Last());
if (!dic.Keys.Contains(liststr.Last()))
{
dic.Add(liststr.Last(), tempeach);
}
}
if (liststr.GroupBy(g => g).ToList().Count < liststr.Count)
{
MessageBox.Show("存在重复");
}
else
{
MessageBox.Show("不存在重复");
}
File.WriteAllLines(@"C:\1.txt", dic.Values.ToArray());//去重复写入文本,默认区第一条