我发现了一个Windows API函数,可以执行字符串的“自然比较”。它的定义如下:
为了在Delphi中使用它,我这样声明它:
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;
我对字符编码知之甚少,这就是我提问的原因。这个函数是否可行?或者我应该先以某种方式转换比较的字符串?