Deleting longest line from txt

佐手、 提交于 2019-12-12 04:16:43

问题


what i'm trying to do here is to delete the longest line from a txt file. Code does it's job, but i also need it to delete multiple "longest lines" and blank lines as well. Any ideas on how to do it?

Code is in C#

    namespace _5_2
    {
    //------------------------------------------------------------
    class Program
    {
         const string CFd = "..\\..\\U1.txt";
         const string CFr = "..\\..\\Results.txt";
         static void Main(string[] args)
         {
             int nr;
             Read(CFd, out nr);
             Print(CFd, CFr, nr);
             Console.WriteLine("Longest line nr. {0, 4:d}", nr + 1);
             Console.WriteLine("Program done");
         }
         //------------------------------------------------------------
         /** Finds number of the longest line.
         @param fv - text file name
         @param nr - number of the longest line */
         //------------------------------------------------------------
         static void Read(string fv, out int nr)
         {
             string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
             int ilgis = 0;
             nr = 0;
             int nreil = 0;
         foreach (string line in lines)
         {
            if (line.Length > ilgis)
               {
                  ilgis = line.Length;
                   nr = nreil;
               }
              nreil++;
          }
        }
         static void Print(string fv, string fvr, int nr)
         {
             string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
             int nreil = 0;
             using (var fr = File.CreateText(fvr))
             {
                 foreach (string line in lines)
                 {
                     if (nr != nreil)
                     {
                         fr.WriteLine(line);
                     }
                     nreil++;
                 }
             }
         }
      }
  }

回答1:


You could identify the longest line, and then loop through the list, deleting all of that length. To also delete empty ones, you could test against String.IsNullOrWhiteSpace.

Something like (pseudocode):

     foreach (string line in lines)
     {
        if (String.IsNullOrWhiteSpace(line)) 
        {
            lines.Delete(line);
            Continue;
        }
        if (line.Length >= longestLine) // ">=" instead of "==" just to be on the safe side
        {
           lines.Delete(line);
        }
    }



回答2:


I would suggest using LINQ. Take advantage of the .Max extension method and iterate over the string array.

string[] lines = { "1", "two", "three" };
var longestLine = lines.Max(line => line.Length);
var revised = lines.Where(line => line.Length < longestLine).ToArray();

The revised variable will contain a string array that excludes the lines with the longest line count.




回答3:


Read lines, filter out empty lines and the 10 longest lines, write lines:

     string[] lines = File.ReadAllLines(inputFile, Encoding.GetEncoding(1257));
     var filtered = lines
         .Where(line => line.Length > 0) // remove all empty lines
         .Except(lines.OrderByDescending(line => line.Length).Take(10)); // remove 10 longest lines
     File.WriteAllLines(outputFile, filtered);


来源:https://stackoverflow.com/questions/33657892/deleting-longest-line-from-txt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!