I have some code that removes duplicate lines from a text file, it then outputs the result (text with no duplicates) to a file. How could I also declare this output as a string as well?
private static void RemoveDuplicate(string sourceFilePath, string destinationFilePath)
{
var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);
File.WriteAllLines(destinationFilePath, readLines.Distinct().ToArray(), Encoding.Default);
}
You could have the method return a string value:
private static string RemoveDuplicate(string sourceFilePath, string destinationFilePath)
{
var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);
var result = readLines.Distinct().ToArray();
File.WriteAllLines(destinationFilePath, result, Encoding.Default);
return string.Join(Environment.NewLine, result);
}
and then:
string result = RemoveDuplicate("source.txt", "dest.txt");
String should be returned, not string array so,use String.join, you can use your own delimiter to seperate lines, i used comma, you could use a new line.
private static string RemoveDuplicate(string sourceFilePath, string destinationFilePath)
{
var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);
var result = readLines.Distinct().ToArray();
string resultString = String.Join(",",ids);
File.WriteAllLines(destinationFilePath, result, Encoding.Default);
return resultString;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.