下面是一个计算字符串中汉字个数的程序;楼主参考下;var ans : AnsiString;wis : WideString;sub : Integer; //汉字的个数Beginans := '盗版delphi';wis := WideString( ans );sub := Length( ans ) - Length( wis );End;
<<<<现炒现卖if length(edit1.Text)=length(widestring(edit1.Text)) then showmessage('edit1只有汉字')
//这个函数返回有斗筲个汉字 function CalcUnicodeLen(const S: string):integer; var i,j:integer; begin j:=0; For i:= 1 to Length(Trim(s)) do begin if ByteType(s,i)<> mbTrailByte then inc(j); end; Result := j; end;
一不小心贴错了,再来 //这个函数返回有多少个汉字 function CalcUnicodeLen(const S: string):integer; var i,j:integer; begin j:=0; For i:= 1 to Length(Trim(s)) do begin if ByteType(s,i) = mbLeadBytethen inc(j); end; Result := j; end;
showmessage('edit1只有汉字')
function CalcUnicodeLen(const S: string):integer;
var
i,j:integer;
begin
j:=0; For i:= 1 to Length(Trim(s)) do
begin
if ByteType(s,i)<> mbTrailByte then
inc(j);
end;
Result := j;
end;
//这个函数返回有多少个汉字
function CalcUnicodeLen(const S: string):integer;
var
i,j:integer;
begin
j:=0; For i:= 1 to Length(Trim(s)) do
begin
if ByteType(s,i) = mbLeadBytethen
inc(j);
end;
Result := j;
end;