delphi中如何判断字符为汉字、英文字母或数字?问题解决即送50分!

解决方案 »

  1.   

    string 存字符串时 汉字为2个字节,其中每个字节的16进制值都大于128。
      

  2.   

    判断数字和英文都好说,汉字就不好搞了.key in ['0'..'9','a'..'z']
    这是判断数字和英文
      

  3.   

    unit gb2big5;interface
      function GBtoBIG5(value: string): string;
      function BIG5toGB(value: string): string;implementationvar BIG5Order : array[0..14757] of Word;
        GBOrder : array[0..8177] of Word;function GBOffset(value: string): integer;
    begin
      if length(value)>=2 then
         Result:=(Ord(value[1])-161)*94+(Ord(value[2])-161)
      else
         Result:=-1;
    end;function BIG5Offset(value: string): integer;
    begin
      if length(value)>=2 then
      begin
           if (Ord(value[2])>=64) and (Ord(value[2])<=126) then
              Result:=(Ord(value[1])-161)*157+(Ord(value[2])-64);
           if (Ord(value[2])>=161) and (Ord(value[2])<=254) then
              Result:=(Ord(value[1])-161)*157+63+(Ord(value[2])-161);
      end
      else
         Result:=-1;
    end;function WordToString(value: Word): string;
    begin
      Result:=Chr(Hi(value))+Chr(Lo(Value));
    end;function isBIG5(value: string): Boolean;
    begin
      if (length(value)>=2) then
      begin
           if (value[1]<#161) then
              Result:=false
           else
              if ((value[2]>=#64) and (value[2]<=#126)) or ((value[2]>=#161) and (value[2]<=#254)) then
                 Result:=true
              else
                 Result:=false;
      end
      else
         Result:=false;
    end;function isGB(value: string): Boolean;
    begin
      if (length(value)>=2) then
      begin
           if (value[1]<=#161) and (value[1]>=#247) then
              Result:=false
           else
              if (value[2]<=#161) and (value[2]>=#254) then
                 Result:= false
              else
                 Result:=true;
      end
      else
         Result:=true;
    end;function GBtoBIG5(value: string): string;
    var leng, idx : integer;
        tmpStr : string[2];
        Offset : integer;
        output : string;
    begin
      output:='';
      leng:=length(value);
      idx:=1;
      while idx<=leng do
      begin
           tmpStr:=value[idx]+value[idx+1];
           if isGB(tmpStr) then
           begin
                offset:=GBOffset(tmpStr);
                if (offset>=0) and (offset<=8177) then
                begin
                     output:=output+WordToString(GBOrder[offset]);
                     inc(idx);
                end
                else
                   output:=output+value[idx] ;
           end
           else
              output:=output+value[idx] ;       inc(idx,1);
      end;
      Result:=output;
    end;function BIG5toGB(value: string): string;
    var leng,idx : integer;
        tmpStr : string[2];
        output : string;
        offset : integer;
    begin
      output:='';
      leng:=length(value);
      idx:=1;
      while idx<=leng do
      begin
           tmpStr:=value[idx]+value[idx+1];
           if isBIG5(tmpStr) then
           begin
                offset:=BIG5Offset(tmpStr);
                if (offset>=0) and (offset<=14757) then
                begin
                     output:=output+WordToString(BIG5Order[offset]);
                     inc(idx);
                end
                else
                  output:=output+value[idx];
           end
           else
             output:=output+value[idx];       inc(idx);
      end;
      Result:=output;
    end; 大富翁裡面找的
      

  4.   

    if ch in LeadByte then 
      It is Far East Language includes chinese
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      str:string;
      I:Integer;
    begin
      str:='Ads6f.,你好2';
      I:=1;
      while I<=Length(str) do
      begin
         if  Integer(str[I])>$a0  then
         begin
              ShowMessage('汉字');
              Inc(I);
         end
         else if str[I] in ['0'..'9'] then
             ShowMessage('数字')
         else if str[I] in ['a'..'z','A'..'Z'] then
             ShowMessage('字母')
         else ShowMessage('非法字符');
         Inc(I);
      end;
    end;
    可以这样判断,具体你要怎么弄自己写过
      

  6.   

    简单点的...
    if char in ['0'..'9'] then
      //数字
    if char in ['A'..'Z','a'..'z'] then
      //字母
    if ord(char)>255 then
      inc(i);
      {i/2就是汉字,这个办法很简陋..}
      

  7.   

    If (ByteType(copy(StrTemp, 1, 24), 24) = mbSingleByte) //判断最后一个字节
                Or (ByteType(copy(StrTemp, 1, 24), 24) = mbTrailByte) Then
                  Begin //单字母 //双字节中第二位    //直接打印
                    Printer.Canvas.TextOut(pstX_Bank, pstY_Bank +
                      (i - 1) * h + 1, copy(StrTemp, 1, 24));
                    StrTemp:= copy(StrTemp, 25, Length(StrTemp) - 24);
                  End
                Else
                  If ByteType(copy(StrTemp, 1, 24), 24) = mbLeadByte Then //双字节中第一位
                    Begin //留下一行打印
                      Printer.Canvas.TextOut(pstX_Bank, pstY_Bank +
                        (i - 1) * h + 1, copy(StrTemp, 1, 23));
                      StrTemp:= copy(StrTemp, 24, Length(StrTemp) - 23);
                    End;这是我自己的程序中用的一段代码,看看能不能帮上你的忙,其实就是用ByteType判断字符是什么,是单个字符还是汉字的后一位,就可以知道是单字符的数字,字母,还是双字节汉字了
      

  8.   

    用一个String存储字符串 如果 ord(string[奇数位])>128 为汉字,否则ASCII,因为汉字的第一个字节大于128
      

  9.   

    Case Str[1] of
      '0'..'9':
         showmessage('数字');
      'a'..'z','A'..'Z':
         showmessage('英文');
    else
         showmessage('汉字');
    end;