//////////////////////////////////////// 例 利用文本控件Tmemo来存放文章,分别对中、英文的字符数进行统计,我们可以通过把字符转换为ASCII码数值来进行判断,Ord()函数就可以把字符转换为对应的数值,值33-126为键盘可使用字符,值127以上的为未知字符,即为汉字 procedure TForm1.Button1Click(Sender: Tobject); var s:string; I,e,c:integer; begin s:=memo1.text; e:=0;c:=0; for I:=1 to length(s) do begin if (ord(s[I])>=33)and(ord(s[I])<=126) then begin inc(e); label1.caption:='英文字数:'+inttostr(e); end else if (ord(s[I])>=127) then begin inc(c); label2.caption:='中文字数:'+inttostr(c div 2); end; end; end;最简单的办法是: length(yourstring)-length(widestring(yourstring))=汉字个数 2*length(widestring(yourstring))-length(yourstring)=英语个数
c:integer; Str:string; astr1:Pchar; astr:Char; begin c:=1; while c<strlen(pchar(Str))+1 do begin astr1:=pchar(copy(Str,c,1)); astr:=astr1^; if ord(astr)>128 then begin //是汉字; end else begin //非汉字; end; end; end;
SORRY 倒数第二个end 前 差一句 c:=c+1;
for i:=0 to length(tempstr)-1 do begin if bytetype(tempstr[i],1)<>mbsinglebyte then Showmessage('有中文'); end;
将句子中的汉字分离出来 procedure TForm1.Button1Click(Sender: TObject); var str: string; i: integer; temp : string; begin str := 'i love you 中国'; temp := ''; i := 0; while i < length(str) do begin while IsDBCSLeadByte(byte(str[i])) do begin temp := temp + widestring(str[i]+str[i+1]); i := i+2; end; inc(i); end; showmessage(temp); end;
例
利用文本控件Tmemo来存放文章,分别对中、英文的字符数进行统计,我们可以通过把字符转换为ASCII码数值来进行判断,Ord()函数就可以把字符转换为对应的数值,值33-126为键盘可使用字符,值127以上的为未知字符,即为汉字
procedure TForm1.Button1Click(Sender: Tobject);
var s:string;
I,e,c:integer;
begin
s:=memo1.text;
e:=0;c:=0;
for I:=1 to length(s) do
begin
if (ord(s[I])>=33)and(ord(s[I])<=126) then
begin
inc(e);
label1.caption:='英文字数:'+inttostr(e);
end
else
if (ord(s[I])>=127) then
begin
inc(c);
label2.caption:='中文字数:'+inttostr(c div 2);
end;
end;
end;最简单的办法是:
length(yourstring)-length(widestring(yourstring))=汉字个数
2*length(widestring(yourstring))-length(yourstring)=英语个数
Str:string;
astr1:Pchar;
astr:Char;
begin
c:=1;
while c<strlen(pchar(Str))+1 do
begin
astr1:=pchar(copy(Str,c,1));
astr:=astr1^;
if ord(astr)>128 then
begin
//是汉字;
end
else
begin
//非汉字;
end;
end;
end;
倒数第二个end 前
差一句 c:=c+1;
begin
if bytetype(tempstr[i],1)<>mbsinglebyte then
Showmessage('有中文');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
i: integer;
temp : string;
begin
str := 'i love you 中国';
temp := '';
i := 0;
while i < length(str) do
begin
while IsDBCSLeadByte(byte(str[i])) do
begin
temp := temp + widestring(str[i]+str[i+1]);
i := i+2;
end;
inc(i);
end;
showmessage(temp);
end;