delphi字符串区分中英文判断

1.string类型的写法

 ik1  :=32//管制定行长度
 while  Length(sLongSendStr) >ik1 do
 begin
      sSet :=  Copy(sLongSendStr,ik1,1); //取得最后一字符
      iCHk :=  GetText(sSet,1);

       case iCHk  of
	       0:begin
	             iSet :=ik1;   //单字母则刚好
	         end;
	       1:begin
	              iSet := ik1 -1 ; //双字节第1位
	         end;
	       2:begin
	               iSet :=ik1;  //双字节第二位则刚好
	          end;
        end;

        sSendStr :=Copy(sLongSendStr,1,iSet);
        mmLog.Lines.Add(sSendStr);

        iLen     := Length(sSendStr);
        iSendLen := Length(sLongSendStr);

       sLongSendStr  := Copy(sLongSendStr,iSet,iSendLen);

       if  Length(sLongSendStr) < ik1 then      mmLog.Lines.Add(sLongSendStr);
end 
//先判断要截取的字符串最后一个字节的类型
//如果为汉字的第一个字节则减(加)一位
 //mbSingleByte   单字母
 //mbLeadByte    双字节第一位
 //mbTrailByte   双字节第二位
  if ByteType(strtxt,iLen) = mbSingleByte then
  begin
      result := 0;  //单字母
  end else
  begin
     if  ByteType(strtxt,iLen) = mbTrailByte then
     begin
        result :=2;  //双字节第2位
     end else
     begin 
        //mbTrailByte
        result :=1;  //双字节第1位
     end;

  end;

2.widestring

widestring类型一个汉字是一个字节,不在是string的双字节。

用ord ( sLongSendStr[k]) >$7F 判断是否为中文字!!!
因为string类型,我部分字符简繁体判断时存在中文字的双字节第一位还是第二位的误判情况,查询了很多方式,最后将字符串变为widestring 来判断。

var 
    sLongSendStr,s2 :widestring;

 ik1 :=16;    //WIDE STRING 汉字只为一个字节;若string 则是双字节
                if Length(lsStr) >ik1 then
                begin
                  sLongSendStr:=lsStr;
                  iLong := Length(sLongSendStr);
                   while length(sLongSendStr) >0 do  //仍然有长度!!!
                   begin

                      iSendLen:=length(lsStr);
                      //sLongSendStr :=  lsStr;
                      j:=1;
                      k:=1;
                      while  j<=32 do
                       begin
                         if ord ( sLongSendStr[k])  >$7F   then
                           begin
                             str:=str+copy(sLongSendStr,j,2);    //  ord(str[i]) > $7F
                             if (j+2) >32 then
                             begin
                                iLen := k-1;
                                s1   := Copy(sLongSendStr,1,iLen);

                                s2 := s1;

                                sLongSendStr := Copy(sLongSendStr,iLen+1,iLong);
                                mmLog.Lines.Add(s2);
                             end;
                             j:=j+2;
                           end
                         else
                         begin
                           if (j+1) >32 then
                           begin
                              iLen := k-1;
                              s1           := Copy(sLongSendStr,1,iLen);
                              sLongSendStr := Copy(sLongSendStr,iLen+1,iLong);
                              mmDisplayMsg.Lines.Add(s1);
                           end;
                           j:=j+1;
                         end;
                         inc(k);
                       end;
                   end;

                   if   length(sLongSendStr) >0 then
                        mmLog.Lines.Add(sLongSendStr);

                   lsStr :='';


                end else
                begin
                   mmLog.Lines.Add(lsStr);
                end;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值