Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何显示文本文件中的每个名称最后一行_C# - Fatal编程技术网

C# 如何显示文本文件中的每个名称最后一行

C# 如何显示文本文件中的每个名称最后一行,c#,C#,我想显示每个名字的最后一行。文本文件中有许多名称,一个名称包含50多行。我想显示文本文件中最后一行的每个名称 请检查这个链接,你们都会明白的 有像黄油和苹果这样的名字。黄油有9行,苹果有22行。我想展示黄油的第9行和苹果的第22行 提前谢谢 我用这段代码来显示所有的行,但我想显示第9行和苹果中的第22行 static void Main(string[] args) { int counter = 0; string line; // Read the file an

我想显示每个名字的最后一行。文本文件中有许多名称,一个名称包含50多行。我想显示文本文件中最后一行的每个名称

请检查这个链接,你们都会明白的

有像黄油和苹果这样的名字。黄油有9行,苹果有22行。我想展示黄油的第9行和苹果的第22行

提前谢谢

我用这段代码来显示所有的行,但我想显示第9行和苹果中的第22行

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();  
}
public静态字符串返回Namebetween命令(字符串str)
{
int indexOfTheFirstComma=str.IndexOf(',');//查找第一个逗号索引
string strafterfthefirstcomma=str.SubString(indexOfTheFirstComma+1);//在第一个逗号之后取其余的
indexOfTheFirstComma=strAfterTheFirstComma.IndexOf(',');
第一个逗号的子字符串(0,第一个逗号的索引-1)后面的字符串TheNameBetweenFirstWoCommas=Strafter;
第一个逗号后面的返回列;
}
公共类名称和事件
{
公共字符串名称{get;set;}
公共整数出现{get;set;}
}
公共静态无效方法()
{
List listOfLines=新列表();
//逐行阅读列表中的所有行,使用列表行。将(行)每行添加为列表的元素
List listOfDistinctNames=新列表();
foreach(列表行中的var项)
{
行中的字符串名称=返回命令(项)之间的名称;
如果(!listOfDistinctNames.Contains(NameInLine))
{
添加(名称行中的名称)的区别名称列表;
}
}
List ListofNameAndOccurrence=新列表();
foreach(差异名称列表中的var名称TR)
{
NameAndOccurrence nameAndOccr=新名称AndOccurrence();
int发生率=0
foreach(listOfLines中的var lineStr)
{
if(lineStr.Contains(nameStr))
{
发生++;
}
其他的
{
nameandcr.Name=nameStr;
nameandcr.occurrence=发生率;
名称和事件列表。添加(名称和OCCR);
}
}
}
//现在你已经知道了行中的名称以及它们出现的次数
//例如,对于您显示的图像,有10行名称为Butter-1M的行
//所以在你的名字和事件列表中,你有这样一个对象
//名称和发生率。名称为“黄油-1M”,名称和发生率。发生率为10
//你也为苹果买了一个。
//在那之后,你要做的就是为Butter-1M编写文本文件的第10行
}

清楚吗?

那么,你想做什么?我不明白你想做什么,文本文件中的所有数字是什么意思?我想显示Apple、butter、LastLines,我需要为List of NameandOccurrence=new List()添加这些引用@SharrokG您不需要任何设置,只是类名为NameandOccurrence,但在创建新实例时,它名为NameandOccurrence^\u^。@SharrokG我从类名中删除了一个c,它必须是正确的。我应该如何调用它们?请帮助我更新您的代码
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

}