【发布时间】:2016-12-22 10:26:21
【问题描述】:
我有一个名为GetServerName 的函数。我需要传递文件名(例如“test.txt”)以及所需的部分字符串(例如“服务器”)
test.txt 文件包含类似这样的内容
data1 | abcd
data2 | efgh
server| 'serverName1'
data3 | ijkl
我需要提取服务器名称,所以在我的函数中我将传递类似GetServerName('test.txt', 'server') 的内容,它应该返回serverName1。
我的问题是 test.txt 之前是一个 ANSI 编码的文件。现在它可以是 ANSI 编码文件或 Unicode 编码文件。下面的函数对 ANSI 编码的文件正常工作,但如果文件以 UNICODE 编码,则会出现问题。我怀疑有 LoadStringsFromFile 功能的东西。因为当我调试时,我可以看到它返回 Unicode 字符而不是人类可读的字符。如何简单地解决我的问题? (或者如何找到我的文件的编码类型以及如何将UNICODE字符串转换为ANSI进行比较,然后我可以自己做)
function GetServerName(const FileName, Section: string): string;
//Get Smartlink server name
var
DirLine: Integer;
LineCount: Integer;
SectionLine: Integer;
Lines: TArrayOfString;
//Lines: String;
AHA: TArrayOfString;
begin
Result := '';
if LoadStringsFromFile(FileName, Lines) then
begin
LineCount := GetArrayLength(Lines);
for SectionLine := 0 to LineCount - 1 do
begin
AHA := StrSplit(Trim(Lines[SectionLine]), '|')
if AHA[0] = Section then
begin
Result := AHA[1];
Exit;
end
end
end;
end;
编辑
在 Windows 中,当我另存为文本文件时。我在图像中附加了 4 个选项。我找到了,Windows 提到 unicode 为 UTF-16LE 编码(有点混乱)
【问题讨论】:
-
Unicode 不是编码。 unicode 是一个字符集。编码是UTF-8, UTF-16, UTF-32 等。你用的是哪一个?关于检测:文件的Unicode版本是否有BOM?
-
谢谢@MartinPrikryl。请检查我上面的编辑。
-
所以使用 UTF-8(它也是 Unicode,尽管记事本没有这么说;UTF = Unicode 转换格式)。
LoadStringsFromFile不支持 UTF-16(在记事本中被混淆地称为 Unicode) -
@MartinPrikryl ,UTF-8 有效。你能把它作为答案吗?
-
我的回答说你应该从一开始就使用UTF-8。
标签: unicode inno-setup ansi