vb.net 替换双双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15481198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace Double Double Quotes
提问by Internet Engineer
I need to replace double double quotes but not replace fields with empty values.
我需要替换双双引号而不是用空值替换字段。
Question: How can I replace double double quotes and not replace empty columns?
问题:如何替换双双引号而不替换空列?
Original Line
原线
""COTTAGE"","PENNINGTON, NJ","","123456789"
""小屋"","新泽西州彭宁顿","","123456789"
Expected outcome
预期结果
"COTTAGE","PENNINGTON, NJ","","123456789"
"小屋","新泽西州彭宁顿","","123456789"
I have created a class that will do the trick, but it replaces all double double quotes, including the empty field:
我创建了一个可以解决问题的类,但它替换了所有双双引号,包括空字段:
My code outcome
我的代码结果
"COTTAGE","PENNINGTON, NJ",","123456789"
"小屋","新泽西州彭宁顿",","123456789"
as you can see my code fixes COTTAGE, but kills the empty column after ,NJ to have a single double quote.
正如您所看到的,我的代码修复了 COTTAGE,但在 ,NJ 之后杀死了空列以获得单双引号。
Public Class 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 FunctionEnd Class
公共类 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结束班
采纳答案by Meta-Knight
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

