我正在创建一个 CSV 文件以上传到一个网站,如果有这么多未转义的句号,该文件就会很适合。
在试图找出哪些字段需要转义时,我很高兴学习一些正则表达式,但这毫无意义吗?是否有任何理由不将每个字段都用双引号括起来并完成它?
这似乎有点矫枉过正,但也许搜索每个字符串就更加矫枉过正了,呵呵。
这是我的功能:
Private Sub WriteToCSVFile(ByVal s As String(,), ByVal sFileName As String)
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 To s.GetUpperBound(0)
For j As Integer = 0 To s.GetUpperBound(1)
If Regex.Match(s(i, j), "^[\w ]*$").Success Then
sb.Append(s(i, j) & IIf(j = s.GetUpperBound(1), "", ","))
Else
sb.Append("""" & s(i, j) & IIf(j = s.GetUpperBound(1), """", ""","))
End If
Next
sb.AppendLine()
Next
System.IO.File.WriteAllText(Application.StartupPath & "\" & sFileName, sb.ToString)
End Sub