我有一个名为GetServerName
的函数。我需要传入文件名(例如'test.txt')以及所需部分字符串(例如'server')。
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;