gpt4 book ai didi

delphi - Delphi中是否需要将字符串转换为WideString?

转载 作者:行者123 更新时间:2023-12-03 14:34:48 24 4
gpt4 key购买 nike

我发现了一个 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;

我对字符编码知之甚少,所以这就是我问题的原因。这个函数可以吗?或者我应该首先以某种方式转换两个比较的字符串?

最佳答案

请记住,将字符串转换为 WideString 将使用默认的系统代码页对其进行转换,这可能是也可能不是您需要的。通常,您希望使用当前用户的区域设置。

来自 System.pas 中的 WCharFromChar:

Result := MultiByteToWideChar(DefaultSystemCodePage, 0, CharSource, SrcBytes,
WCharDest, DestChars);

您可以通过调用SetMultiByteConversionCodePage来更改DefaultSystemCodePage .

关于delphi - Delphi中是否需要将字符串转换为WideString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1024515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com