【问题标题】:Help with parsing lines of a file and replacing a substring帮助解析文件的行和替换子字符串
【发布时间】:2011-07-19 19:20:52
【问题描述】:

我需要替换文件每一行中的某些字符。文件没有分隔,但每一行都有固定的格式。例如,我需要将 5 个问号替换为“x”。每行需要替换的5个问号在第10位找到。所以例如:

输入文件:

abdfg trr?????456
g?????dhs?????diu
eerrttyycdhjjhddd

输出文件应该是:

abdfg trrxxxxx456
g?????dhsxxxxxdiu
eerrttyycdhjjhddd

输出文件将作为不同文件保存到特定位置

在 VB.NET 中执行此操作的最佳方法是什么(我对 VB 有点陌生,所以任何代码示例都会有所帮助)?

【问题讨论】:

  • 每个文件总是相同的,还是文件之间不同?另外,问号的数量总是相同吗?
  • 是的,问号的数量总是相同的,并且总是出现在相同的索引上(每行相同)。每个文件都不一样,但是格式是一样的……把每行第10位的5个问号换成xxxxx。

标签: .net regex vb.net file parsing


【解决方案1】:

一种可能的解决方案是使用StreamReader(通过ReadLine() 方法)解析文件中的每一行。当您阅读每一行时,您可以使用StreamWriter 将原始行写出(通过WriteLine(String) 方法),只需进行一次调整。如果该行满足您的替换要求,您将使用String.Replace(String, String) 方法将旧字符串替换为替换字符串。

这是一个解决方案(使用上面的示例数据编译和测试)。您仍然希望添加一些异常处理(至少,确保文件首先存在):

Public Shared Sub ReplacementExample(string originalFile, string newFile)

    ' Read contents of "oringalFile"
    ' Replace 5 ? characters with 5 x characters.
    ' Write output to the "newFile"
    ' Note: Only do so if the ? characters begin at position 10


    Const replaceMe As String = "?????"
    Const replacement As String = "xxxxx"

    Dim line As String = Nothing

    Using r As New StreamReader(originalFile)
        Using w As New StreamWriter(newFile)

            line = r.ReadLine()

            While Not line Is Nothing
                w.WriteLine(line.Substring(0, 9) + _
                            line.Substring(9).Replace(replaceMe, replacement))

                line = r.ReadLine()
            End While

        End Using
    End Using
End Sub

【讨论】:

  • 您可能需要稍微尝试一下异常处理。如果文件丢失,这可能会失败,因为流是在 try/catch 块之外定义的。不过你明白了。
  • @Probhu:没问题。我更新了一个更好的例子......没有意识到现在在 VB.NET 中使用了块。
  • 进一步清理了答案。不妨让它对以后可能阅读本文的其他人有用。
【解决方案2】:

根据提供的源代码位置判断是9

C#代码:

var res = s.Substring(0, 9) + s.Substring(9).Replace("?????", "xxxxx");

VB.NET:

Dim res As String = (s.Substring(0, 9) & s.Substring(9).Replace("?????", "xxxxx"))

VB.NET 示例:

Using sr As StreamReader = New StreamReader("a.txt")
        Using sw As StreamWriter = New StreamWriter("b.txt")
            Dim line As String = Nothing
            Do While (Not line = sr.ReadLine Is Nothing)
                Dim res As String = (line.Substring(0, 9) & line.Substring(9).Replace("?????", "xxxxx"))
                sw.WriteLine(res)
            Loop
        End Using
    End Using

示例 C#:

using (var sr = new StreamReader("a.txt"))
{
    using (var sw = new StreamWriter("b.txt"))
    {
        string line = null;
        while ((line = sr.ReadLine()) != null)
        {
            var res = line.Substring(0, 9) + line.Substring(9).Replace("?????", "xxxxx");
            sw.WriteLine(res);
        }
    }
}

【讨论】:

  • 所以只需阅读每一行并为这些索引做一个 string.replace ?不需要转换成数组什么的吧?
  • 对不起,如果我不清楚,但我不想替换所有出现的 ?????在线(可能有多个)。我只想替换出现在位置/索引 10 中的那些。
  • 这取决于文件大小。如果它很小,例如File.ReadAllText,则替换字符串,写入输出。如果文件很大,按行读取/替换/写入更合适。
  • @Prabhu,再次更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多