我找到了一个执行字符串“自然比较”的 Windows API 函数。定义如下:
int StrCmpLogicalW(
LPCWSTR psz1,
LPCWSTR psz2
);
为了在 Delphi 中使用它,我这样声明:
interface
function StrCmpLogicalW(psz1, psz2: PWideChar): integer; stdcall;
implementation
function StrCmpLogicalW; external 'shlwapi.dll' name 'StrCmpLogicalW';
因为它比较Unicode字符串,所以当我想比较 ANSI 字符串时,我不确定如何调用它。将字符串转换为 WideString 然后转换为 PWideChar 似乎就足够了,但是,我不知道这种方法是否正确:
function AnsiNaturalCompareText(const S1, S2: string): integer;
begin
Result := StrCmpLogicalW(PWideChar(WideString(S1)), PWideChar(WideString(S2)));
end;
我对字符编码知之甚少,所以这就是我提出问题的原因。这个函数可以吗,还是我应该先以某种方式转换两个比较的字符串?