【问题标题】:VB Remove blocks of text in textfileVB删除文本文件中的文本块
【发布时间】:2018-07-10 12:51:20
【问题描述】:

在我使用 C# 和使用 sytemIO 的简单问题之后,最近切换到 vb。我的前任写了一个将错误日志生成到文本文件的包。以下为示例:


2017-10-20 15:30:11.481 CmsMonitorService.exe、CmsMonitorService.UpdateCmsOffLine OffLineUpdater 错误:获取存储在离线保管库中的文件列表。 ------------------------------ 2017-10-20 15:31:11.547 CmsMonitorService.exe、CmsMonitorService.UpdateCmsOffLine OffLineUpdater 错误:创建文件夹“OffLineUpdates”(它可能已经存在 checkHost) 在 CmsMonitorService.CmsMonitorService.UpdateCmsOffLine(Object[] Args) ------------------------------ 2017-10-20 15:31:11.547 CmsMonitorService.exe、CmsMonitorService.UpdateCmsOffLine OffLineUpdater 错误:创建文件夹 ------------------------------

但这正在杀死机器。代码当前在编写时所做的是逐行删除内容,这非常缓慢。它使用以下内容:

Do
 If allLines.Count = 0 Then
    Exit Do
 ElseIf allLines(0).StartsWith("-----") Then
    allLines.RemoveAt(0)
     Exit Do
 Else
    allLines.RemoveAt(0)
 End If
Loop

可能有数千个(它们分布在不同的位置)。

我想做的是找到一种方法来删除破折号之间的块。

感谢大家的任何想法.....

加雷斯

【问题讨论】:

  • 对不起,伙计们......这个帖子在发布时非常混乱。迫不及待地想重新做......
  • 如果您将全部内容存储在单个String 中,那么您可以使用IndexOf 查找您的一系列破折号的第一次和第二次出现,然后删除其间的子字符串。实际上最有效的方法是创建一个String 和一个StringBuilder 并向后工作。我的意思是在String 上调用LastIndexOf 以查找最后一次和倒数第二次出现,然后在StringBuilder 上调用Replace 以删除该子字符串。对倒数第二次和倒数第三次再次执行相同操作,直到不再出现。
  • 谢谢你... :)

标签: vb.net file text block edit


【解决方案1】:

这是我在上面的评论中描述的解决方案的一个简单示例:

Imports System.Text

Module Module1

    Sub Main()
        Dim s = "keep line 1" & Environment.NewLine &
                "keep line 2" & Environment.NewLine &
                "----------" & Environment.NewLine &
                "remove line 1" & Environment.NewLine &
                "remove line 2" & Environment.NewLine &
                "----------" & Environment.NewLine &
                "keep line 3" & Environment.NewLine &
                "keep line 4" & Environment.NewLine &
                "----------" & Environment.NewLine &
                "remove line 3" & Environment.NewLine &
                "remove line 4" & Environment.NewLine &
                "----------" & Environment.NewLine &
                "keep line 5" & Environment.NewLine &
                "keep line 6" & Environment.NewLine
        Dim sb As New StringBuilder(s)

        Dim endIndex = s.LastIndexOf("----------")

        Do While endIndex <> -1
            Dim startIndex = s.LastIndexOf("----------", endIndex - 1)
            Dim substring = s.Substring(startIndex, endIndex - startIndex + 12) 'Add the length of the delimiter and the line break.

            'Remove the delimited block from the StringBuilder.
            sb.Replace(substring, String.Empty, startIndex, substring.Length)

            endIndex = s.LastIndexOf("----------", startIndex - 1)
        Loop

        Console.WriteLine("Before:")
        Console.WriteLine(s)
        Console.WriteLine("After:")
        Console.WriteLine(sb.ToString())

        Console.ReadLine()
    End Sub

End Module

这可能取决于文本的具体情况,是否更有效或仅使用String 更有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多