delphi字符串中汉字个数统计

unit mainunit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button

unit mainunit;


interface


uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;


type

  TForm1 = class(TForm)

    Button1: TButton;

    Label1: TLabel;

    Edit1: TEdit;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

  procedure CountChineseChars(SpecStr:String);

  procedure UseByteType(SpecStr:String);

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.dfm}

procedure TForm1.CountChineseChars(SpecStr:String);

var

 WSStr:WideString;

 TempStr:String;

 I,E,C,O:integer;

begin

 WSStr:=SpecStr;

 E:=0;

 C:=0;

 O:=0;

 for I:=1 to Length(WSStr) do

  begin

   if (ord(WSStr[I])>=33)and(ord(WSStr[I])<=127) then

    begin

     TempStr:=WSStr[I];

     if (TempStr[1] in ['a'..'z','A'..'Z']) then

      Inc(E)

     else

      Inc(O);

    end

    else

    begin

     if (ord(WSStr[I])>=127) then

      begin

       Inc(C);

      end;

    end;

  end;

 Application.MessageBox(Pchar('该字符串的组成结构为:汉字'+IntToStr(C)+'个,'+'英文字母'+IntToStr(E)+'个,'+'其他类型字符'+IntToStr(O)+'个'),'使用WideString数据类型分析字符串组成结构',mb_ok+mb_iconinformation);

end;


procedure TForm1.UseByteType(SpecStr:String);

var

 I,E,C,O:integer;

begin

 E:=0;

 C:=0;

 O:=0;

 for I:=1 to Length(SpecStr) do

  begin

   if (ByteType(SpecStr,I)=mbSingleByte) then

    begin

     if (SpecStr[I] in ['a'..'z','A'..'Z']) then

      Inc(E)

     else

      Inc(O);

    end

    else

     Inc(C);

  end;

 Application.MessageBox(Pchar('该字符串的组成结构为:汉字'+IntToStr(C div 2)+'个,'+'英文字母'+IntToStr(E)+'个,'+'其他类型字符'+IntToStr(O)+'个'),'使用ByteType方法分析字符串组成结构',mb_ok+mb_iconinformation);

end;


procedure TForm1.Button1Click(Sender: TObject);

begin

 CountChineseChars(Edit1.Text);

 Edit1.SetFocus;

 Edit1.SelectAll;

end;


procedure TForm1.Button2Click(Sender: TObject);

begin

 UseByteType(Edit1.Text);

end;


end.


相关推荐

[!--temp.pl--]