字符串数组替换不更新原始字符串

String array replace not updating original string

我正在尝试替换文件中的所有双引号,但是当我尝试更新字符串数组时,我只是再次获取原始行而不是清理后的字符串。 (ReplaceQuotes 函数中的布尔值仅用于测试,当行中有 " 时它们返回 true)。如果我查看 cleanLine 字符串,引号已被删除,但是当我 return fileContent 数组时, 它看起来就像带引号的原件。

 Private Sub CleanFile(currentFileInfo As FileInfo)
    Dim fullName As String = currentFileInfo.FullName
    Dim fileContent As String() = GetFileContent(currentFileInfo.FullName)
    Dim cleanFileContent As String() = ReplaceQuotes(fileContent)
End Sub

Private Function GetFileContent(fileName As String) As String()
    Return System.IO.File.ReadAllLines(fileName)
End Function

Private Function ReplaceQuotes(fileContent As String())

    For Each line As String In fileContent
        Dim cleanLine As String
        Dim quoteTest As Boolean
        quoteTest = line.Contains("""")
        Dim quoteTest2 As Boolean = line.Contains(ControlChars.Quote)
        cleanLine = line.Replace(ControlChars.Quote, "")
        line = cleanLine
    Next

    Return fileContent

End Function

您必须重新分配原始数组中的新字符串,而不是替换局部字符串变量。因此,您不能使用 For Each,只能使用 For 循环。而且方法可以更短:

Private Sub ReplaceQuotes(fileContent As String())
    For i As Int32 = 0 To fileContent.Length - 1
        fileContent(i) = fileContent(i).Replace(ControlChars.Quote, "")
    Next
End Sub