delphi ByteType-单双字节判断


本文整理自网络,侵删。

 //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-单双字节判断

相关阅读 >>

Delphi 生成随机字符串

Delphi 时间转成大写

Delphi 操作sql 插入一万条数据 三种方式速度测试

Delphi 以系统权限运行程序的代码

Delphi删除文件

Delphi kbmmemtable的简单应用(增删改查示例)

idhttpserver允许跨域访问

Delphi mainmenu控件 checkde属性用法

Delphi 在长文件名和短文件名之间转换

Delphi执行sql提示“不正常地定义参数对象”,“提供了不一致或不完整的信息”错误

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...