// 获取指定汉字的拼音索引字母,如:“汉”的索引字母是“H” function GetPYIndexChar( hzchar:string):char; begin case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of $B0A1..$B0C4 : result := 'A'; $B0C5..$B2C0 : result := 'B'; $B2C1..$B4ED : result := 'C'; $B4EE..$B6E9 : result := 'D'; $B6EA..$B7A1 : result := 'E'; $B7A2..$B8C0 : result := 'F'; $B8C1..$B9FD : result := 'G'; $B9FE..$BBF6 : result := 'H'; $BBF7..$BFA5 : result := 'J'; $BFA6..$C0AB : result := 'K'; $C0AC..$C2E7 : result := 'L'; $C2E8..$C4C2 : result := 'M'; $C4C3..$C5B5 : result := 'N'; $C5B6..$C5BD : result := 'O'; $C5BE..$C6D9 : result := 'P'; $C6DA..$C8BA : result := 'Q'; $C8BB..$C8F5 : result := 'R'; $C8F6..$CBF9 : result := 'S'; $CBFA..$CDD9 : result := 'T'; $CDDA..$CEF3 : result := 'W'; $CEF4..$D188 : result := 'X'; $D1B9..$D4D0 : result := 'Y'; $D4D1..$D7F9 : result := 'Z'; else result := char(0); end; end;// 在指定的字符串列表SourceStrs中检索符合拼音索引字符串 PYIndexStr的所有字符串,并返回。 function SearchByPYIndexStr ( SourceStrs:TStrings; PYIndexStr:string):string; label NotFound; var i, j :integer; hzchar :string; begin for i:=0 to SourceStrs.Count-1 do begin for j:=1 to Length(PYIndexStr) do begin hzchar:=SourceStrs[i][2*j-1] + SourceStrs[i][2*j]; if (PYIndexStr[j]<>'?') and (UpperCase(PYIndexStr[j]) <> GetPYIndexChar(hzchar)) then goto NotFound; end; if result='' then result := SourceStrs[i] else result := result + Char (13) + SourceStrs[i]; NotFound: end; end;
楼上是个常用的方法,再给你贴两个,第二个和楼上差不多: 1. function GetHzPy(const AHzStr: string): string; 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 i, j, HzOrd: integer; begin := 1; while i <= Length(AHzStr) do begin if (AHzStr[i] >= #160) and (AHzStr[i + 1] >= #160) then begin HzOrd := (Ord(AHzStr[i]) - 160) * 100 + Ord(AHzStr[i + 1]) - 160; for j := 0 to 25 do begin if (HzOrd >= ChinaCode[j][0]) and (HzOrd <= ChinaCode[j][1]) then begin Result := Result + char(byte('a') + j); break; end; end; Inc(i); end else Result := Result + AHzStr[i]; Inc(i); end; end; 2. function GetEn(CnString: string): string; function GetEnChar(cnchar: string): char; begin case Word(cnchar[1]) shl 8 + Word(cnchar[2]) of $B0A1..$B0C4: result := 'a'; $B0C5..$B2C0: result := 'b'; $B2C1..$B4ED: result := 'c'; $B4EE..$B6E9: result := 'd'; $B6EA..$B7A1: result := 'e'; $B7A2..$B8C0: result := 'f'; $B8C1..$B9FD: result := 'g'; $B9FE..$BBF6: result := 'h'; $BBF7..$BFA5: result := 'j'; $BFA6..$C0AB: result := 'k'; $C0AC..$C2E7: result := 'l'; $C2E8..$C4C2: result := 'm'; $C4C3..$C5B5: result := 'n'; $C5B6..$C5BD: result := 'o'; $C5BE..$C6D9: result := 'p'; $C6DA..$C8BA: result := 'q'; $C8BB..$C8F5: result := 'r'; $C8F6..$CBF9: result := 's'; $CBFA..$CDD9: result := 't'; $CDDA..$CEF3: result := 'w'; $CEF4..$D188: result := 'x'; $D1B9..$D4D0: result := 'y'; $D4D1..$D7F9: result := 'z'; else result := char(0); end; end; var i, len: integer; begin CnString := StringReplace(CnString, ' ', '', [rfReplaceAll]); //删除空格 len := Length(CnString) - Length(WideString(CnString)); //汉字个数 for i := 1 to len do result := result + GetEnChar(copy(CnString, 2*i-1, 2)); end;
function GetPYIndexChar( hzchar:string):char;
begin
case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of
$B0A1..$B0C4 : result := 'A';
$B0C5..$B2C0 : result := 'B';
$B2C1..$B4ED : result := 'C';
$B4EE..$B6E9 : result := 'D';
$B6EA..$B7A1 : result := 'E';
$B7A2..$B8C0 : result := 'F';
$B8C1..$B9FD : result := 'G';
$B9FE..$BBF6 : result := 'H';
$BBF7..$BFA5 : result := 'J';
$BFA6..$C0AB : result := 'K';
$C0AC..$C2E7 : result := 'L';
$C2E8..$C4C2 : result := 'M';
$C4C3..$C5B5 : result := 'N';
$C5B6..$C5BD : result := 'O';
$C5BE..$C6D9 : result := 'P';
$C6DA..$C8BA : result := 'Q';
$C8BB..$C8F5 : result := 'R';
$C8F6..$CBF9 : result := 'S';
$CBFA..$CDD9 : result := 'T';
$CDDA..$CEF3 : result := 'W';
$CEF4..$D188 : result := 'X';
$D1B9..$D4D0 : result := 'Y';
$D4D1..$D7F9 : result := 'Z';
else
result := char(0);
end;
end;// 在指定的字符串列表SourceStrs中检索符合拼音索引字符串
PYIndexStr的所有字符串,并返回。
function SearchByPYIndexStr
( SourceStrs:TStrings;
PYIndexStr:string):string;
label NotFound;
var
i, j :integer;
hzchar :string;
begin
for i:=0 to SourceStrs.Count-1 do
begin
for j:=1 to Length(PYIndexStr) do
begin
hzchar:=SourceStrs[i][2*j-1]
+ SourceStrs[i][2*j];
if (PYIndexStr[j]<>'?') and
(UpperCase(PYIndexStr[j]) <>
GetPYIndexChar(hzchar)) then goto NotFound;
end;
if result='' then result := SourceStrs[i]
else result := result + Char
(13) + SourceStrs[i];
NotFound:
end;
end;
1.
function GetHzPy(const AHzStr: string): string;
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
i, j, HzOrd: integer;
begin
:= 1;
while i <= Length(AHzStr) do
begin
if (AHzStr[i] >= #160) and (AHzStr[i + 1] >= #160) then
begin
HzOrd := (Ord(AHzStr[i]) - 160) * 100 + Ord(AHzStr[i + 1]) - 160;
for j := 0 to 25 do
begin
if (HzOrd >= ChinaCode[j][0]) and (HzOrd <= ChinaCode[j][1]) then
begin
Result := Result + char(byte('a') + j);
break;
end;
end;
Inc(i);
end else Result := Result + AHzStr[i];
Inc(i);
end;
end; 2.
function GetEn(CnString: string): string;
function GetEnChar(cnchar: string): char;
begin
case Word(cnchar[1]) shl 8 + Word(cnchar[2]) of
$B0A1..$B0C4: result := 'a';
$B0C5..$B2C0: result := 'b';
$B2C1..$B4ED: result := 'c';
$B4EE..$B6E9: result := 'd';
$B6EA..$B7A1: result := 'e';
$B7A2..$B8C0: result := 'f';
$B8C1..$B9FD: result := 'g';
$B9FE..$BBF6: result := 'h';
$BBF7..$BFA5: result := 'j';
$BFA6..$C0AB: result := 'k';
$C0AC..$C2E7: result := 'l';
$C2E8..$C4C2: result := 'm';
$C4C3..$C5B5: result := 'n';
$C5B6..$C5BD: result := 'o';
$C5BE..$C6D9: result := 'p';
$C6DA..$C8BA: result := 'q';
$C8BB..$C8F5: result := 'r';
$C8F6..$CBF9: result := 's';
$CBFA..$CDD9: result := 't';
$CDDA..$CEF3: result := 'w';
$CEF4..$D188: result := 'x';
$D1B9..$D4D0: result := 'y';
$D4D1..$D7F9: result := 'z';
else
result := char(0);
end;
end;
var
i, len: integer;
begin
CnString := StringReplace(CnString, ' ', '', [rfReplaceAll]); //删除空格
len := Length(CnString) - Length(WideString(CnString)); //汉字个数
for i := 1 to len do
result := result + GetEnChar(copy(CnString, 2*i-1, 2));
end;
$CEF4..$D188: result := 'x'; -> $CEF4..$D1B8: result := 'x';
第一个用起来也很好啊,把数字都考虑进去了