我想显示每个名字的最后一行。文本文件中有许多名称,一个名称包含50多行。我想从文本文件中显示每个名字的最后一行。
请查看此链接,大家都会明白。
有黄油和苹果等名称。黄油包含9行,苹果包含苹果包含22行。我想在苹果中显示第9行黄油和第22行
提前致谢。
我使用此代码显示所有行,但我想在苹果中显示黄油和第22行的第9行。
static void Main(string[] args)
{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\\tra\\20100901.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("apple"))
{
Console.WriteLine(counter.ToString() + ": " + line);
}
counter++;
}
file.Close();
}
答案 0 :(得分:0)
public static string ReturnTheNameBetweenCommas(string str)
{
int indexOfTheFirstComma = str.IndexOf(',');//find the first comma index
string strAfterTheFirstComma = str.SubString(indexOfTheFirstComma +1);//take the rest after the first comma
indexOfTheFirstComma = strAfterTheFirstComma.IndexOf(',');
string theNameBetweenFirstTwoCommas = strAfterTheFirstComma.SubString(0,indexOfTheFirstComma-1);
return strAfterTheFirstComma;
}
public class NameAndOccurence
{
public string Name{ get; set; }
public int Occurence{ get; set; }
}
public static void Method()
{
List<string> listOfLines = new List<string>();
//Read all the lines to listOfLines one by one, use listOfLines.Add(line) every line as an element of the list
List<string> listOfDistinctNames = new List<string>();
foreach(var item in listOfLines)
{
string nameInTheLine = ReturnTheNameBetweenCommas(item);
if(!listOfDistinctNames.Contains(nameInTheLine))
{
listOfDistinctNames.Add(nameInTheLine);
}
}
List<NameAndOccurence> listOfNameAndOccurence = new List<NameAndOccurence>();
foreach(var nameStr in listOfDistinctNames)
{
NameAndOccurence nameAndOccr = new NameAndOccurence();
int occurence = 0
foreach(var lineStr in listOfLines)
{
if(lineStr.Contains(nameStr))
{
occurence++;
}
else
{
nameAndOccr.Name = nameStr;
nameAndOccr.Occurence = occurence;
listOfNameAndOccurence.Add(nameAndOccr);
}
}
}
//now you've got the names in the lines and how many times they occurred
//for example for the image you showed there are 10 lines with the name of Butter-1M
//so in your listOfNameAndOccurence you have an object like this
//nameAndOccurence.Name is "Butter-1M" and nameAndOccurence.Occurence is 10
//you've got one for Apple too.
//Well after that all you gotta do is writing the 10th line of the textfile for Butter-1M
}
是否清楚?