2

我需要替换双双引号,但不要用空值替换字段。

问题:如何替换双双引号而不替换空列?


原线

""小屋"","新泽西州彭宁顿","","123456789"

预期结果

"小屋","新泽西州彭宁顿","","123456789"


我创建了一个可以解决问题的类,但它替换了所有双引号,包括空字段:


我的代码结果

"小屋","新泽西州彭宁顿","123456789"

正如您所看到的,我的代码修复了 COTTAGE,但在 ,NJ 之后删除了空列以使用单个双引号。


公共类 FixFileErrors

Public Shared Sub RemoveDoubleDoubleQuotes(ByVal FilePathandName As String)

    Dim OriginalFile, RevisedFile, LineofText As String

    OriginalFile = FilePathandName
    RevisedFile = Replace(OriginalFile, ".txt", "revised.txt")


    Dim srFileReader As System.IO.StreamReader
    srFileReader = System.IO.File.OpenText(OriginalFile)
    'Dim srFileWritter As System.IO.StreamWriter
    Dim srFileWritter As New System.IO.StreamWriter(RevisedFile)


    Do Until srFileReader.EndOfStream
        LineofText = srFileReader.ReadLine
        LineofText = ReplaceDoubleDoubleQuotes(LineofText)
        srFileWritter.WriteLine(LineofText)
        LineofText = Nothing
    Loop

End Sub

Public Shared Function ReplaceDoubleDoubleQuotes(ByVal Line As String) As String
    Dim NewLine As String


    Dim strFindText As String = Chr(34) & Chr(34)
    Dim strReplaceText As String = Chr(34)

    NewLine = Replace(Line, strFindText, strReplaceText)


    Return NewLine

End Function

结束类

4

1 回答 1

2
Public Shared Function ReplaceDoubleDoubleQuotes(ByVal Line As String) As String
    Dim NewLine As String

    Dim doubleDoubleQuotes As String = Chr(34) & Chr(34)
    Dim doubleQuote As String = Chr(34)

    'Your original replacement
    NewLine = Replace(Line, doubleDoubleQuotes, doubleQuote)

    'Replacement to fix double-quotes between commas
    NewLine = Replace(NewLine, "," & doubleQuote & ",", "," & doubleDoubleQuotes & ",")

    Return NewLine

End Function
于 2013-03-18T18:02:31.420 回答