如何检查字符串在文件中是否唯一,以及是否在C#中写入另一个文件?
我有两个文件F1.txt和F2.txt。F1.txt包含以下内容:如何检查字符串在文件中是否唯一,以及是否在C#中写入另一个文件?,c#,file,C#,File,我有两个文件F1.txt和F2.txt。F1.txt包含以下内容: U1,U2 U1,U5 U3,U4 U2,U1 U3,U4 基本上U1,U2和U2,U1的意思是相同的。所以现在我想把不同的内容写入一个文件F2.txt,也就是说,在写入F2.txt之后应该包含 U1,U2 U1,U5 U3,U4 我尝试了下面的方法,但没有成功 using System; using System.Collections.Generic; using System.IO; using System.Linq
U1,U2
U1,U5
U3,U4
U2,U1
U3,U4
基本上U1,U2和U2,U1的意思是相同的。所以现在我想把不同的内容写入一个文件F2.txt,也就是说,在写入F2.txt之后应该包含
U1,U2
U1,U5
U3,U4
我尝试了下面的方法,但没有成功
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringInFileTechchef
{
class Program
{
static void Main(string[] args)
{
string line = "";
using (StreamReader sr = new StreamReader(@"C:\Users\Chiranjib\Desktop\F1.txt"))
{
while((line=sr.ReadLine()) !=null)
{
if (!File.ReadAllText(@"C:\Users\Chiranjib\Desktop\F2.txt").Contains(line))
{
//char[] array = line.ToCharArray();
//Array.Reverse(array);
//string temp = new string(array);
string temp1 = line.Substring(0, 2);
string temp2 = line.Substring(3, 2);
if (!File.ReadAllText(@"C:\Users\Chiranjib\Desktop\F2.txt").Contains(temp2 + "," + temp1))
{
using (StreamWriter sw = new StreamWriter(@"C:\Users\Chiranjib\Desktop\F2.txt"))
{
sw.WriteLine(line);
Console.WriteLine(line);
}
}
}
}
}
Console.ReadKey();
}
}
}
我错过了什么?如何实现该场景。以下是我将如何实现该场景: 将每一行拆分为一个
字符串[]
对字符串[]进行排序
将字符串[]
连接回字符串
取不同的字符串
var distinct = File.ReadLines("TextFile2.txt")
.Select(l => String.Join(",", l.Split(',').OrderBy(i => i)))
.Distinct();
File.WriteAllLines("F2.txt", distinct);
我会这样做:
将每一行拆分为一个字符串[]
对字符串[]进行排序
将字符串[]
连接回字符串
取不同的字符串
var distinct = File.ReadLines("TextFile2.txt")
.Select(l => String.Join(",", l.Split(',').OrderBy(i => i)))
.Distinct();
File.WriteAllLines("F2.txt", distinct);
另一种方法:
HashSet<string> uniqueLines = new HashSet<string>();
foreach(string line in File.ReadLines("F1.txt"))
{
if (uniqueLines.Contains(line))
continue;
string[] tokens = line.Split(',');
string reversedLine = string.Join(",", tokens.Reverse());
if (uniqueLines.Contains(reversedLine))
continue;
uniqueLines.Add(line);
}
File.WriteAllLines("F2.txt", uniqueLines);
HashSet uniqueLines=newhashset();
foreach(File.ReadLines(“F1.txt”)中的字符串行)
{
if(唯一行。包含(行))
继续;
string[]tokens=line.Split(',');
string reversedLine=string.Join(“,”,tokens.Reverse());
if(唯一行包含(反向行))
继续;
唯一行。添加(行);
}
File.writeAllines(“F2.txt”,uniqueLines);
另一种方法:
HashSet<string> uniqueLines = new HashSet<string>();
foreach(string line in File.ReadLines("F1.txt"))
{
if (uniqueLines.Contains(line))
continue;
string[] tokens = line.Split(',');
string reversedLine = string.Join(",", tokens.Reverse());
if (uniqueLines.Contains(reversedLine))
continue;
uniqueLines.Add(line);
}
File.WriteAllLines("F2.txt", uniqueLines);
HashSet uniqueLines=newhashset();
foreach(File.ReadLines(“F1.txt”)中的字符串行)
{
if(唯一行。包含(行))
继续;
string[]tokens=line.Split(',');
string reversedLine=string.Join(“,”,tokens.Reverse());
if(唯一行包含(反向行))
继续;
唯一行。添加(行);
}
File.writeAllines(“F2.txt”,uniqueLines);
这对我很有效。基本上假设有两个字符串总是以逗号分隔,我只是使用哈希集将它们过滤掉。可能有点过分,但适用于小文件
#region
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#endregion
namespace StringInFileTechchef
{
internal class Program
{
private static void Main(string[] args)
{
HashSet<WordCombo> existingWordCombos = GetWordCombos(File.ReadAllLines(@"C:\Users\Chiranjib\Desktop\F2.txt"));
HashSet<WordCombo> newWordCombos = GetWordCombos(File.ReadAllLines(@"C:\Users\Ganesh\Chiranjib\F1.txt"));
foreach (WordCombo newWordCombo in newWordCombos)
{
existingWordCombos.Add(newWordCombo);
}
StringBuilder stringBuilder = new StringBuilder();
foreach (WordCombo existingWordCombo in existingWordCombos)
{
stringBuilder.AppendFormat("{0},{1}{2}", existingWordCombo.SmallerWord, existingWordCombo.BiggerWord, Environment.NewLine);
}
File.WriteAllText(@"C:\Users\Ganesh\Desktop\F2.txt", stringBuilder.ToString());
}
private static HashSet<WordCombo> GetWordCombos(IEnumerable<string> lines)
{
HashSet<WordCombo> wordCombos = new HashSet<WordCombo>();
foreach (string line in lines)
{
string[] splitWords = line.Split(new[] {','});
wordCombos.Add(new WordCombo(splitWords[0], splitWords[1]));
}
return wordCombos;
}
private class WordCombo
{
public string BiggerWord { get; private set; }
public string SmallerWord { get; private set; }
public WordCombo(string part1, string part2)
{
if (0 < string.Compare(part1, part2, StringComparison.InvariantCultureIgnoreCase))
{
BiggerWord = part1;
SmallerWord = part2;
}
else
{
BiggerWord = part2;
SmallerWord = part1;
}
}
protected bool Equals(WordCombo other)
{
return string.Equals(BiggerWord, other.BiggerWord, StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(SmallerWord, other.SmallerWord, StringComparison.InvariantCultureIgnoreCase);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((WordCombo) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((BiggerWord != null ? BiggerWord.ToLowerInvariant().GetHashCode() : 0)*397) ^
(SmallerWord != null ? SmallerWord.ToLowerInvariant().GetHashCode() : 0);
}
}
}
}
}
#地区
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
#端区
命名空间StringInFileTechchef
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
HashSet existingWordCombos=GetWordCombos(File.ReadAllLines(@“C:\Users\Chiranjib\Desktop\F2.txt”);
HashSet newWordCombos=GetWordCombos(File.ReadAllLines(@“C:\Users\Ganesh\Chiranjib\F1.txt”);
foreach(newWordCombo中的WordCombo newWordCombo)
{
existingWordCombos.Add(newWordCombo);
}
StringBuilder StringBuilder=新的StringBuilder();
foreach(现有WordCombo现有WordCombo中的WordCombo)
{
AppendFormat(“{0},{1}{2}”,existingWordCombo.SmallerWord,existingWordCombo.BiggerWord,Environment.NewLine);
}
writealText(@“C:\Users\Ganesh\Desktop\F2.txt”,stringBuilder.ToString());
}
私有静态HashSet GetWordCombos(IEnumerable行)
{
HashSet wordCombos=新HashSet();
foreach(行中的字符串行)
{
string[]splitWords=line.Split(新[]{',});
添加(新的WordCombo(splitWords[0],splitWords[1]);
}
返回单词组合;
}
私有类WordCombo
{
公共字符串BiggerWord{get;private set;}
公共字符串SmallerWord{get;private set;}
PublicWordCombo(字符串部分1、字符串部分2)
{
if(0
这对我很有效。基本上假设有两个字符串总是以逗号分隔,我只是使用哈希集将它们过滤掉。可能有点过分,但适用于小文件
#region
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#endregion
namespace StringInFileTechchef
{
internal class Program
{
private static void Main(string[] args)
{
HashSet<WordCombo> existingWordCombos = GetWordCombos(File.ReadAllLines(@"C:\Users\Chiranjib\Desktop\F2.txt"));
HashSet<WordCombo> newWordCombos = GetWordCombos(File.ReadAllLines(@"C:\Users\Ganesh\Chiranjib\F1.txt"));
foreach (WordCombo newWordCombo in newWordCombos)
{
existingWordCombos.Add(newWordCombo);
}
StringBuilder stringBuilder = new StringBuilder();
foreach (WordCombo existingWordCombo in existingWordCombos)
{
stringBuilder.AppendFormat("{0},{1}{2}", existingWordCombo.SmallerWord, existingWordCombo.BiggerWord, Environment.NewLine);
}
File.WriteAllText(@"C:\Users\Ganesh\Desktop\F2.txt", stringBuilder.ToString());
}
private static HashSet<WordCombo> GetWordCombos(IEnumerable<string> lines)
{
HashSet<WordCombo> wordCombos = new HashSet<WordCombo>();
foreach (string line in lines)
{
string[] splitWords = line.Split(new[] {','});
wordCombos.Add(new WordCombo(splitWords[0], splitWords[1]));
}
return wordCombos;
}
private class WordCombo
{
public string BiggerWord { get; private set; }
public string SmallerWord { get; private set; }
public WordCombo(string part1, string part2)
{
if (0 < string.Compare(part1, part2, StringComparison.InvariantCultureIgnoreCase))
{
BiggerWord = part1;
SmallerWord = part2;
}
else
{
BiggerWord = part2;
SmallerWord = part1;
}
}
protected bool Equals(WordCombo other)
{
return string.Equals(BiggerWord, other.BiggerWord, StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(SmallerWord, other.SmallerWord, StringComparison.InvariantCultureIgnoreCase);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((WordCombo) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((BiggerWord != null ? BiggerWord.ToLowerInvariant().GetHashCode() : 0)*397) ^
(SmallerWord != null ? SmallerWord.ToLowerInvariant().GetHashCode() : 0);
}
}
}
}
}
#地区
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
#端区
命名空间StringInFileTechchef
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
HashSet existingWordCombos=GetWordCombos(File.ReadAllLines(@“C:\Users\Chiranjib\Desktop\F2.txt”);
HashSet newWordCombos=GetWordCombos(File.ReadAllLines(@“C:\Users\Ganesh\Chiranjib\F1.txt”);
foreach(newWordCombo中的WordCombo newWordCombo)
{
existingWordCombos.Add(newWordCombo);
}
StringBuilder StringBuilder=新的StringBuilder();
foreach(现有WordCombo现有WordCombo中的WordCombo)
{
AppendFormat(“{0},{1}{2}”,existingWordCombo.SmallerWord,existingWordCombo.BiggerWord,Environment.NewLine);
}
Fi