从txt中删除最长的行

Deleting longest line from txt

我在这里要做的是从 txt 文件中删除最长的一行。代码完成了它的工作,但我还需要它来删除多个 "longest lines" 和空行。有什么想法吗?

代码在 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++;
                 }
             }
         }
      }
  }

您可以找出最长的一行,然后遍历列表,删除所有该长度的行。要同时删除空的,您可以针对 String.IsNullOrWhiteSpace.

进行测试

类似于(伪代码):

     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);
        }
    }

我建议使用 LINQ。利用 .Max 扩展方法并迭代字符串数组。

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

修改后的变量将包含一个字符串数组,其中排除了最长行数的行。

读取行,过滤掉空行和最长的10行,写入行:

     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);