加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:15:35 所属栏目:大数据 来源:网络整理
导读:我发现一个Windows API函数执行字符串的“自然比较”。定义如下: int StrCmpLogicalW( LPCWSTR psz1,LPCWSTR psz2); 要在Delphi中使用它,我以这种方式宣布: interface function StrCmpLogicalW(psz1,psz2: PWideChar): integer; stdcall;implementation f
我发现一个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,然后到FlideChar,但是我不知道这种方法是否正确:

function AnsiNaturalCompareText(const S1,S2: string): integer;
begin
  Result := StrCmpLogicalW(PWideChar(WideString(S1)),PWideChar(WideString(S2)));
end;

我对字符编码知之甚少,这是我问题的原因。这个功能是否正常,或者我应该首先将比较的字符串转换成某种方式吗?

解决方法

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

从System.pas中的WCharFromChar:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读