当使用 ANSIContainsText 函数比较两个字符串时,可能会出现不可预料的结果,因为该函数默认使用当前系统代码页而不是 ANSI 字符集进行比较。要解决这个问题,应该使用比较字符串的正确代码页或使用 StrUtils 单元中的 SameStr 方法来代替 ANSIContainsText 函数。
示例代码:
var
str1, str2: AnsiString;
begin
str1 := 'héllo';
str2 := 'HELLO';
// 使用 ANSIContainsText 比较会返回 False
if ANSIContainsText(str1, str2) then
ShowMessage('Strings match');
// 解决方法 1: 指定正确的代码页来比较字符串
if AnsiCompareText(str1, str2, TEncoding.UTF8) = 0 then
ShowMessage('Strings match');
// 解决方法 2: 使用 StrUtils 单元中的 SameStr 方法
if SameStr(str1, str2) then
ShowMessage('Strings match');
end;