Call 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.
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.
Unit
SysUtils
Category
MBCS utilities
function ByteType(const S: string; Index: Integer): TMbcsByteType;
Description
Call 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.
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;
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;
var ans : AnsiString;
wis : WideString;
sub : Integer; //汉字的个数
Begin
ans := '盗版delphi';
wis := WideString( ans );
sub := Length( ans ) - Length( wis );
End;