Delphi获取中文拼音首字母

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}
const py: array[216..247] of string = (
{216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + 'QMBSGZSCYJSYYZPG' +
{216}'KBZGYCYWYKGKLJSW' + 'KPJQHYZWDDZLSGMR' + 'YPYWWCCKZNKYDG',
{217}'TTNJJEYKKZYTCJNM' + 'CYLQLYPYQFQRPZSL' + 'WBTGKJFYXJWZLTBN' +
{217}'CXJJJJZXDTTSQZYC' + 'DXXHGCKBPHFFSSYY' + 'BGMXLPBYLLLHLX',
{218}'SPZMYJHSOJNGHDZQ' + 'YKLGJHXGQZHXQGKE' + 'ZZWYSCSCJXYEYXAD' +
{218}'ZPMDSSMZJZQJYZCD' + 'JEWQJBDZBXGZNZCP' + 'WHKXHQKMWFBPBY',
{219}'DTJZZKQHYLYGXFPT' + 'YJYYZPSZLFCHMQSH' + 'GMXXSXJJSDCSBBQB' +
{219}'EFSJYHXWGZKPYLQB' + 'GLDLCCTNMAYDDKSS' + 'NGYCSGXLYZAYBN',
{220}'PTSDKDYLHGYMYLCX' + 'PYCJNDQJWXQXFYYF' + 'JLEJBZRXCCQWQQSB' +
{220}'ZKYMGPLBMJRQCFLN' + 'YMYQMSQYRBCJTHZT' + 'QFRXQHXMJJCJLX',
{221}'QGJMSHZKBSWYEMYL' + 'TXFSYDSGLYCJQXSJ' + 'NQBSCTYHBFTDCYZD' +
{221}'JWYGHQFRXWCKQKXE' + 'BPTLPXJZSRMEBWHJ' + 'LBJSLYYSMDXLCL',
{222}'QKXLHXJRZJMFQHXH' + 'WYWSBHTRXXGLHQHF' + 'NMCYKLDYXZPWLGGS' +
{222}'MTC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LQKBDMBYLXWCXY' +
{247}'TTYBKMRTJZXQJBHL' + 'MHMJJZMQASLDCYXY' + 'QDLQCAFYWYXQHZ'
    );

function ChnPy(Value: array of char): Char;
begin
  Result := #0;
  case Byte(Value[0]) of
    176:
      case Byte(Value[1]) of
        161..196: Result := 'A';
        197..254: Result := 'B';
      end; {case}
    177:
      Result := 'B';
    178:
      case Byte(Value[1]) of
        161..192: Result := 'B';
        193..205: Result := 'C';
        206: Result := 'S'; //参
        207..254: Result := 'C';
      end; {case}
    179:
      Result := 'C';
    180:
      case Byte(Value[1]) of
        161..237: Result := 'C';
        238..254: Result := 'D';
      end; {case}
    181:
      Result := 'D';
    182:
      case Byte(Value[1]) of
        161..233: Result := 'D';
        234..254: Result := 'E';
      end; {case}
    183:
      case Byte(Value[1]) of
        161: Result := 'E';
        162..254: Result := 'F';
      end; {case}
    184:
      case Byte(Value[1]) of
        161..192: Result := 'F';
        193..254: Result := 'G';
      end; {case}
    185:
      case Byte(Value[1]) of
        161..253: Result := 'G';
        254: Result := 'H';
      end; {case}
    186:
      Result := 'H';
    187:
      case Byte(Value[1]) of
        161..246: Result := 'H';
        247..254: Result := 'J';
      end; {case}
    188..190:
      Result := 'J';
    191:
      case Byte(Value[1]) of
        161..165: Result := 'J';
        166..254: Result := 'K';
      end; {case}
    192:
      case Byte(Value[1]) of
        161..171: Result := 'K';
        172..254: Result := 'L';
      end; {case}
    193:
      Result := 'L';
    194:
      case Byte(Value[1]) of
        161..231: Result := 'L';
        232..254: Result := 'M';
      end; {case}
    195:
      Result := 'M';
    196:
      case Byte(Value[1]) of
        161..194: Result := 'M';
        195..254: Result := 'N';
      end; {case}
    197:
      case Byte(Value[1]) of
        161..181: Result := 'N';
        182..189: Result := 'O';
        190..254: Result := 'P';
      end; {case}
    198:
      case Byte(Value[1]) of
        161..217: Result := 'P';
        218..254: Result := 'Q';
      end; {case}
    199:
      Result := 'Q';
    200:
      case Byte(Value[1]) of
        161..186: Result := 'Q';
        187..245: Result := 'R';
        246..254: Result := 'S';
      end; {case}
    201..202:
      Result := 'S';
    203:
      case Byte(Value[1]) of
        161..249: Result := 'S';
        250..254: Result := 'T';
      end; {case}
    204:
      Result := 'T';
    205:
      case Byte(Value[1]) of
        161..217: Result := 'T';
        218..254: Result := 'W';
      end; {case}
    206:
      case Byte(Value[1]) of
        161..243: Result := 'W';
        244..254: Result := 'X';
      end; {case}
    207..208:
      Result := 'X';
    209:
      case Byte(Value[1]) of
        161..184: Result := 'X';
        185..254: Result := 'Y';
      end; {case}
    210..211:
      Result := 'Y';
    212:
      case Byte(Value[1]) of
        161..208: Result := 'Y';
        209..254: Result := 'Z';
      end; {case}
    213..215:
      Result := 'Z';
    216..247:
      Result := py[Byte(Value[0])][Byte(Value[1]) - 160];
  end; {case}
end;

function ChnToPY(Value: string): string;
var
  I, L: Integer;
  C: array[0..1] of char;
  R: Char;
begin
  Result := '';
  L := Length(Value);
  I := 1;
  while I <= (L - 1) do
  begin
    if Value[I] < #160 then
    begin
      Result := Result + Value[I];
      Inc(I);
    end
    else
    begin
      C[0] := Value[I];
      C[1] := Value[I + 1];
      R := ChnPY(C);
      if r <> #0 then
        Result := Result + R;
      Inc(I, 2);
    end;
  end;
  if I = L then
    Result := Result + Value[L];
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  edit2.Text := ChnToPY(trim(edit1.Text));
end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值