Convert special characters to TStringList
I am using Delphi 7 and have a subroutine that takes a csv file with a series of records and imports them. This is done by loading it into a TStringList with MyStringList.LoadFromFile (csvfile) and then for each line with a line = MyStringList [i].
This always worked fine, but now I find that the special characters are not matched correctly. For example, Rue François Coppée comes out as Rue Franñois Coppà © e - percussive French characters - problem.
Is there an easy way to solve this problem?
source to share
Your file is encoded as UTF-8. For example, consider ç . As you can see from the link, this is encoded in UTF-8 as 0xC3 0xA7
. And in Windows-1252 , 0xC3
encodes à and 0xA7
encodes §
.
Whether or not this can be easily dealt with using ANSI Delphi depends on the prevailing code page under which your program is running.
- If you are using Windows 1252 then you should be fine. You just need to decode the UTF-8 encoded text with a call
UTF8Decode
. - If you use a different language, life becomes more difficult. These characters may not be present in your locale character set, in which case you cannot represent them in a Delphi variable
string
that is encoded using the prevailing ANSI encoding. If so, you need to use Unicode.
If you are processing international text, you need to either:
- Migrating to modern Delphi with Unicode support, or
- Stick with Delphi 7 and use
WideString
TNT Unicode components as well.
source to share