一个关于字符串的问题!!在线待待!

Go_home 2005-03-31 04:01:21
字符串A:='这是个测试程序,this's a test.这是个测试程序啊';
请问如何获得A中的汉字数和非汉字数,即有多少个单字节数和双字节数
...全文
146 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyinwuhan 2005-03-31
  • 打赏
  • 举报
回复
>>D6中写明了S从下标0开始计算!!
呵呵,看看D7怎么写的?
Description

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.
MMas 2005-03-31
  • 打赏
  • 举报
回复
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.

D6中写明了S从下标0开始计算!!
honkiko 2005-03-31
  • 打赏
  • 举报
回复
对该字符串进行循环,每次读一个char,也就是一个字节。如果该字节的ASCII值大于127,则说明这个字节以及随后的字节是一个汉字,累计值加一后跳过后面的一个字节,继续循环。
算法很简单,就不用我写了吧
senfore 2005-03-31
  • 打赏
  • 举报
回复
你这样不好理解吧,

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;
何鲁青 2005-03-31
  • 打赏
  • 举报
回复
聪明...
flyinwuhan 2005-03-31
  • 打赏
  • 举报
回复
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;
flyinwuhan 2005-03-31
  • 打赏
  • 举报
回复
var ans : AnsiString;
wis : WideString;
sub : Integer; //汉字的个数
Begin
ans := '盗版delphi';
wis := WideString( ans );
sub := Length( ans ) - Length( wis );
End;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧