var s:string; s:='x'; //一个字符 if length(s)=2 then //汉字 else //英文
//判断是否汉字: Function isHZ(ZF:String):Boolean; //这里只接受一个汉字的判断 var HZF:WideString; Begin if length(zf)=2 then //因为一个汉字 有两个半角字符组成 Begin HZF:=ZF; isHZ:=(length(HZF)=1); //因为WideString;类型一个汉字长度也是1 end else isHZ:=False; end;Function ChinaCodeToAbc(StrEditText: String): string; { 把汉字转换成拼音字母 StrEditText : 要转换的汉字。 } Const Chinacode:array[0..25,0..1] of integer=((1601,1636), (1637,1832),(1833,2077),(2078,2273),(2274,2301),(2302,2432), (2433,2593),(2594,2786),(9999,0000),(2787,3105),(3106,3211), (3212,3471),(3472,3634),(3635,3722),(3723,3729),(3730,3857), (3858,4026),(4027,4085),(4086,4389),(4390,4557),(9999,0000), (9999,0000),(4558,4683),(4684,4924),(4925,5248),(5249,5589)); var StrResult:string; I,J,intEditTextlength:integer; WdcodeNumber:word; begin StrResult:=''; I:=1; intEditTextlength:=length(StrEditText); while (I<=intEditTextlength) do begin if (ord(StrEditText[I])>=160) and (ord(StrEditText[I+1])>=160) then begin WdcodeNumber:=(ord(StrEditText[I])-160)*100+ord(StrEditText[I+1])-160; J:=0; while (J<=25) do begin if (WdcodeNumber>=chinacode[J,0]) and (WdcodeNumber<=chinacode[J,1]) then begin StrResult:=StrResult+chr(J+ord('A')); break; end; J:=J+1; end; I:=I+2 end else begin StrResult:=StrResult+uppercase(StrEditText[I]); //英文代码 I:=I+1; end; end; Result:=StrResult; End;
最简单的办法是: length(yourstring)-length(widestring(yourstring))=汉字个数 2*length(widestring(yourstring))-length(yourstring)=英语个数 //////////////////////////////////////// 中国使用的是汉字,而又夹杂这些英文,这样我们在数字数时就麻烦了,电脑是按字节来计算,一个汉字算两个字,而中国人的习惯是一个汉字就是一个字,所以通过电脑来计算必须解决下列问题: 利用文本控件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;
有些汉字的第二个字节ascii值小于127,看我的方法 var i,j:integer; s:string; begin s:='单t字e符s和t汉字组合字符串'; l:=6; //预设分割点 i:=1; while i<=length(copy(s,1,l)) do begin if ord(copy(s,1,l)[i])>127 then begin if i=l then inc(l); i:=i+2; end else inc(i); end; 字串1:=copy(s,1,l); 字串2:=copy(s,l+1,length(s)-l); end;
s:string;
s:='x'; //一个字符
if length(s)=2 then
//汉字
else
//英文
Function isHZ(ZF:String):Boolean;
//这里只接受一个汉字的判断
var HZF:WideString;
Begin
if length(zf)=2 then
//因为一个汉字 有两个半角字符组成
Begin
HZF:=ZF;
isHZ:=(length(HZF)=1);
//因为WideString;类型一个汉字长度也是1
end
else isHZ:=False;
end;Function ChinaCodeToAbc(StrEditText: String): string;
{ 把汉字转换成拼音字母
StrEditText : 要转换的汉字。
}
Const Chinacode:array[0..25,0..1] of integer=((1601,1636),
(1637,1832),(1833,2077),(2078,2273),(2274,2301),(2302,2432),
(2433,2593),(2594,2786),(9999,0000),(2787,3105),(3106,3211),
(3212,3471),(3472,3634),(3635,3722),(3723,3729),(3730,3857),
(3858,4026),(4027,4085),(4086,4389),(4390,4557),(9999,0000),
(9999,0000),(4558,4683),(4684,4924),(4925,5248),(5249,5589));
var
StrResult:string;
I,J,intEditTextlength:integer;
WdcodeNumber:word;
begin
StrResult:='';
I:=1;
intEditTextlength:=length(StrEditText);
while (I<=intEditTextlength) do
begin
if (ord(StrEditText[I])>=160) and (ord(StrEditText[I+1])>=160) then
begin
WdcodeNumber:=(ord(StrEditText[I])-160)*100+ord(StrEditText[I+1])-160;
J:=0;
while (J<=25) do
begin
if (WdcodeNumber>=chinacode[J,0]) and (WdcodeNumber<=chinacode[J,1]) then
begin
StrResult:=StrResult+chr(J+ord('A'));
break;
end;
J:=J+1;
end;
I:=I+2
end else
begin
StrResult:=StrResult+uppercase(StrEditText[I]); //英文代码
I:=I+1;
end;
end;
Result:=StrResult;
End;
length(yourstring)-length(widestring(yourstring))=汉字个数
2*length(widestring(yourstring))-length(yourstring)=英语个数
////////////////////////////////////////
中国使用的是汉字,而又夹杂这些英文,这样我们在数字数时就麻烦了,电脑是按字节来计算,一个汉字算两个字,而中国人的习惯是一个汉字就是一个字,所以通过电脑来计算必须解决下列问题:
利用文本控件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;
var
i,j:integer;
s:string;
begin
s:='单t字e符s和t汉字组合字符串';
l:=6; //预设分割点
i:=1;
while i<=length(copy(s,1,l)) do begin
if ord(copy(s,1,l)[i])>127 then begin
if i=l then inc(l);
i:=i+2;
end
else inc(i);
end;
字串1:=copy(s,1,l);
字串2:=copy(s,l+1,length(s)-l);
end;