【问题标题】:VB.net How to remove quotes characters from a streamReader.readline.split()VB.net 如何从 streamReader.readline.split() 中删除引号字符
【发布时间】:2016-11-22 00:06:52
【问题描述】:

我已经建立了一个从报告中读取数据的项目,它曾经可以正常工作,但现在由于某种原因,报告将所有内容都放入了字符串中。所以我想修改我的流阅读器以在读取行时删除或忽略引号。

这是读取行的部分的片段。

Dim RawEntList As New List(Of Array)()

Dim newline() As String
Dim CurrentAccountName As String
Dim CurrentAccount As Account
Dim AccountNameExsists As Boolean
Dim NewAccount As Account
Dim NewEntry As Entrey
Dim WrongFileErrorTrigger As String

ListOfLoadedAccountNames.Clear()
'opens the file
Try
    Dim sr As New IO.StreamReader(File1Loc)
    Console.WriteLine("Loading full report please waite")
    MsgBox("Loading full report may take a while.")
    'While we have not finished reading the file
    While (Not sr.EndOfStream)
        'spliting eatch line up into an array
        newline = sr.ReadLine.Split(","c)
        'storring eatch array into a list
        RawEntList.Add(newline)
    End While

然后我当然会遍历列表以提取信息来填充对象,如下所示:

For Each Entr In RawEntList
    CurrentAccountName = Entr(36)
    AccountNameExsists = False
    For Each AccountName In ListOfLoadedAccountNames
        If CurrentAccountName = AccountName Then
            AccountNameExsists = True
        End If
    Next

【问题讨论】:

    标签: vb.net replace trim streamreader


    【解决方案1】:

    你可以这样做

    StringName.Replace(ControlChars.Quote, "")
    

    StringName.Replace(Chr(34), "")
    

    streamReader.readline.split().Replace(Chr(34), "")
    

    【讨论】:

    • 它不是一个字符串,它是一个字符串数组,这就是 split() 的工作方式。并且您不能在数组上调用替换。我想我可以像 ent(0).Replace(Char(34), "") 这样在数组中的每个值上调用它,并且必须经过 50 次。女巫已经在另一个运行了 150,000 次或更多的过滤器中运行。它可以摆姿势,但效率很低。所以这种排除了你的前两种解决方案,但我可能会尝试第三种解决方案,看看它是否有效。
    • @SkylineGodzilla :所以在打电话给.Split() 之前输入.Replace()。虽然后者可能返回一个数组,但ReadLine() 不会:sr.ReadLine().Replace(Chr(34), "").Split(","c)
    • @WallyModz :或StringName.Replace(""""c, "")。 ;)
    • @SkylineGodzilla 你可以做一个 foreach 或者按照 Visual Vincent 所说的去做。
    • 每个循环的 A 需要更多的迭代,这是他不想要的。编辑您的答案并为您的示例提供与我相同的结构 (sr.ReadLine().Replace(...).Split(...))。这样,所有这些都将在他的情况下工作。
    【解决方案2】:

    在拆分之前,在 readline 之后进行替换怎么样?这应该可以节省迭代乘法,或者更好(如果可能的话),使用 File 对象的 ReadAllText 方法对整个文件进行替换(如果数据以可以完成的方式格式化并且您有足够的内存),请执行你的替换,然后从内存中读取行来构建你的数组(超级快!)。

    File.ReadAllText(路径)

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多