字符串A:='这是个测试程序,this's a test.这是个测试程序啊';
请问如何获得A中的汉字数和非汉字数,即有多少个单字节数和双字节数

解决方案 »

  1.   

    var ans : AnsiString;
        wis : WideString;
        sub : Integer; //汉字的个数
    Begin
      ans := '盗版delphi';
      wis := WideString( ans );
      sub := Length( ans ) - Length( wis );
    End;
      

  2.   

    var
      s : string;     i, j:integer;
    begin
      s:='这是个测试程序,this''''s a test.这是个测试程序啊';
      j:=length(s);
      for i:=1 to length(s) do
        if ByteType( s, i ) = mbLeadByte then dec( j);
      showmessage('汉字数:'+inttostr(length(s)-j));
    end;
      

  3.   

    你这样不好理解吧,var
      s : string;     i, j:integer;
    begin
      s:='这是个测试程序,this''''s a test.这是个测试程序啊';
      j:=0
      for i:=1 to length(s) do
        if ByteType( s, i ) = mbLeadByte then inc( j);
      showmessage('汉字数:'+inttostr(j));
    end;
      

  4.   

    对该字符串进行循环,每次读一个char,也就是一个字节。如果该字节的ASCII值大于127,则说明这个字节以及随后的字节是一个汉字,累计值加一后跳过后面的一个字节,继续循环。
    算法很简单,就不用我写了吧
      

  5.   

    Indicates 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 utilitiesfunction ByteType(const S: string; Index: Integer): TMbcsByteType;DescriptionCall ByteType to determine the type of the byte specified by the Index parameter, where 0 specifies the first byte in S, 1 specifies the second byte, and so on.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 the second byte of a double byte 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.D6中写明了S从下标0开始计算!!
      

  6.   

    >>D6中写明了S从下标0开始计算!!
    呵呵,看看D7怎么写的?
    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.