Jack
Jack

Reputation: 119

Replace special characters in text file. Best way to do this?

I've got a text file with lines and special characters. I want to replace those. Is this the best way to do this? I've got the feeling this is not the quickest way.

Dim lOpenFile As Long
Dim sFileText As String
Dim sFileName As String

sFileName = "C:\test.txt"


lOpenFile = FreeFile
Open sFileName For Input As lOpenFile
sFileText = Input(LOF(lOpenFile), lOpenFile)
Close lOpenFile

sFileText = Replace(sFileText, " Ç ", " C ")
sFileText = Replace(sFileText, " ü ", " u ")
sFileText = Replace(sFileText, " é ", " e ")


lOpenFile = FreeFile
Open sFileName For Output As lOpenFile
Print #lOpenFile, sFileText
Close lOpenFile

Also i want to sum up things that has been changed. Anyone who can help me with that?

Upvotes: 1

Views: 3796

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460340

I guess what you actually want is to remove all diacretics from a string, so accents or umlauts. Then you could use this method which is also efficient since it's using a StringBuilder:

Public Shared Function RemoveDiacritics(s As String) As String
    Dim normalizedString As String = s.Normalize(NormalizationForm.FormD)
    Dim stringBuilder As New StringBuilder()

    For Each c As Char In normalizedString
        If CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark Then
            stringBuilder.Append(c)
        End If
    Next

    Return stringBuilder.ToString()
End Function

[ remember to add Imports System.Text to the top of your code file ]

But note that it will convert an ü to u(as desired) which isn't correct. Normally you should "translate" german umlauts in the following way: ü=ue,ä=ae, ö=oe. Related

You'd call above method f.e. in this way:

Dim text As String = File.ReadAllText(sFileName)
Dim newText As String = RemoveDiacritics(text)
File.WriteAllText(sFileName, newText)

Upvotes: 4

Related Questions