主页 > 编程资料 > Delphi >
发布时间:2015-09-22 作者:网络 阅读:112次

//ByteType:判断是单字节还是双字节;
procedure TForm9.BitBtn3Click(Sender: TObject);
var s:System.AnsiString;
  iCount:Integer;
  c:AnsiString;
begin
s:='China Bank(中国银行)';
//ByteType:针对的是采用MBCS字符集的数据类型,对Unicode字符集无效,总是会返回mbSingleByte;
for iCount := 1 to System.Length(s) do
  begin
    //mbSingleByte:单字节;
    //mbLeadByte:双字节前半;
    //mbTrailByte:双字节后半;
    if SysUtils.ByteType(s,iCount)=mbSingleByte then
       Memo1.Lines.Add(s[iCount])
    else
      if SysUtils.ByteType(s,iCount)=mbLeadByte then
         begin
           c:=System.Copy(s,iCount,2);
           Memo2.Lines.Add(c);
         end;
  end;
end;
//附图:
ByteType-单双字节判断 - 朝三暮四 - 朝三暮四的博客
//统计汉字个数;
procedure TForm9.BitBtn3Click(Sender: TObject);
var s:System.AnsiString;
  iCount:Integer;
  c:AnsiString;
begin
s:='China Bank(中國银行)';
//s在内存的储存形式是:s[n]...s[1]s[0];计算机的CPU是从右向左方向进行读取;
for iCount := 1 to System.Length(s) do
  begin
    if (SysUtils.ByteType(s,iCount)=mbLeadByte) then
       //GBK汉字编码部分分三片区:
       //第一片区(GBK/2:GB2312部分):高字节范围从$A1..$FE,低字节范围从$B0..$F7;
       //第二片区(GBK/3:扩充汉字部分):高字节范围从$40..$FE,低字节范围从$81..$A0;
       //第三片区(GBK/4:扩充汉字部分):高字节范围从$40..$A0,低字节范围从$AA..$FE;
       if ((PByte(@s[iCount+1])^ in [$A1..$FE]) and (PByte(@s[iCount])^ in [$B0..$F7])) or
          ((PByte(@s[iCount+1])^ in [$40..$FE]) and (PByte(@s[iCount])^ in [$81..$A0])) or
          ((PByte(@s[iCount+1])^ in [$40..$A0]) and (PByte(@s[iCount])^ in [$AA..$FE])) then
          begin
            c:=System.Copy(s,iCount,2);
            Memo1.Lines.Add(c);
          end;
  end;
end;
//附图:
ByteType-单双字节判断
关键字词: