CuiCuiTaGE 发表于 2019-12-3 10:42

返回中文的拼音首字母

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;

type
TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function CnPYIndex(const CnString: string): string;   //返回中文的拼音首字母
const
ChinaCode: array 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
i, j, HzOrd, l_iHz: Integer;
Hz: AnsiString;
begin
i := 1;
l_iHz := 1;
while i <= Length(CnString) do
begin
    Hz := CnString;
    if (Hz >= #160) and (Hz >= #160) then
    begin
      HzOrd := (Ord(Hz) - 160) * 100 + Ord(Hz) - 160;
      for j := 0 to 25 do
      begin
      if (HzOrd >= ChinaCode) and (HzOrd <= ChinaCode) then
      begin
          Result := Result + Char(Byte('A') + j); //改A为a就全部是小写
          Break;
      end;
      end;
    end
    else
      Result := Result + CnString;
    Inc(i);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   ShowMessage(CnPYIndex(Edit1.Text));
end;

end.

diyikuai 发表于 2019-12-3 23:53

厉害厉害,我只会用易语言搞这个

xcrane 发表于 2020-4-12 16:17

测试了一下,这个里面的汉字是不全的。
比如:"蓓" 这个字,就找不到他对应的拼音。

LQpJlqFNXxE 发表于 2022-2-23 01:56

谢谢分享

Ail9274 发表于 2022-2-23 02:18

谢谢分享

fFEH0 发表于 2022-2-28 13:08

谢谢分享

UpjaxeJ67380 发表于 2022-2-28 22:42

被标题吸引进来了,回复看看

IPGVh84539 发表于 2022-3-1 20:05

学习永不停歇

KUp9281 发表于 2022-4-13 23:44

感谢楼主

UmVDK 发表于 2022-4-23 08:50

甘拜下风,谢谢分享
页: [1] 2 3 4 5 6 7
查看完整版本: 返回中文的拼音首字母