我的程序中设置一个edit 如何判断text中的内容是字符还是汉字  ,哪位有详细的程序  谢谢

解决方案 »

  1.   

    要考虑几种情况的,一种是纯英文的,一种是纯中文的,还有一种是中英混合的。不过,都可以用 ByteType 函数来进行判断的。ndicates whether a byte in a string is a single byte character, the first byte of a double byte character, or the second byte of a double byte character.UnitSysUtilsCategoryMBCS utilitiesDelphi syntax:function ByteType(const S: string; Index: Integer): TMbcsByteType;C++ syntax:extern PACKAGE TMbcsByteType __fastcall ByteType(const AnsiString S, int Index);DescriptionCall ByteType to determine whether a specified byte in a string is a single-byte character, the first byte of a multibyte character, or one of the trailing bytes.AnsiString is the string that contains the byte in question.Index identifies the byte for which you want to know the byte type. Bytes are numbered from 1.If the system is not using a multi-byte character system (MBCS), ByteType always returns mbSingleByte. Otherwise, ByteType returns mbSingleByte if the indicated byte represents a complete character in S, mbLeadByte if it represents the first byte of a double byte character, and mbTrailByte if it represents a subsequent byte of a multibyte character.Note: No checking is done to ensure that Index is less than the length of S. It is the caller's responsibility to ensure that Index is not out of bounds.
      

  2.   

    ByteType('你好haha吗',1) = mbLeadByte;   //双字节字符的第一个字符
    ByteType('你好haha吗',2) = mbTrailByte;  //双字节字符的第二个字符
    ByteType('你好haha吗',5) = mbSingleByte; //单字节字符
      

  3.   

    var
    a,b:char;
    c,d:string;
    i:integer;
    begin
    edit2.Text:='';
    for i:=0 to length(edit1.Text)-1 do
    begin
    a:=pchar(copy(edit1.Text,1+i*2,1))[0];
    b:=pchar(copy(edit1.Text,2+i*2,1))[0];
    c:=copy(edit1.Text,1+i*2,2);
    if (ord(a)>128) and (ord(b)>128) then
    begin
    d:=d+c;
    end;
    end;
    edit2.Text:=edit2.Text+d;
    end;