我需要替换双引号,但不能用空值替换字段。
问题:如何替换双引号而不替换空列?
Original Line
“”COTTAGE“”,“PENNINGTON,NJ”,“”,“123456789”
预期结果
“COTTAGE”,“PENNINGTON,NJ”,“”,“123456789”
我创建了一个可以解决这个问题的类,但它取代了所有双引号,包括空字段:
我的代码结果
“COTTAGE”,“PENNINGTON,NJ”,“,”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
结束班
答案 0 :(得分: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