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

时间:2015-01-06 15:13:05

标签: arrays vb.net string

我正在尝试替换文件中的所有双引号,但是当我尝试更新字符串数组时,我只是再次获取原始行而不是清理后的字符串。 (ReplaceQuotes函数中的布尔值仅用于测试,当它出现在行中时它们会返回true。如果我查看cleanLine字符串,引号已被删除,但是当我返回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

1 个答案:

答案 0 :(得分:2)

您必须在原始数组中重新分配新字符串,而不是替换本地字符串变量。因此,您无法使用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