事实上,解决这个问题的思路很简单。那就是要找出汉字表中拼音首字母分别为“A”到“Z”的汉字内码范围,这样对于要检索的汉字,只需要检查它的内码位于哪一个首字母的范围内,就可以判断出它的拼音首字母。
下面完成一个示范程序,选择“File|New Application”菜单命令,在默认的窗体中添加1个Label组件、1个Bevel组件、1个Edit组件和2个ListBox组件。完成对各个组件的属性设置及布局之后,在Edit组件的OnChange事件中添加如下所示的代码。
 程序代码
使用拼音首字母序列实现检索功能Procedure TForm1.Edit1Change(Sender: TObject);
使用拼音首字母序列实现检索功能var ResultStr:
string;
使用拼音首字母序列实现检索功能begin
使用拼音首字母序列实现检索功能ResultStr:
='';
使用拼音首字母序列实现检索功能listbox2.Items.Text :
= SearchByPYIndexStr(listbox1.Items, edit1.Text);
使用拼音首字母序列实现检索功能end;

这里所使用的SearchByPYIndexStr函数的代码如下所示,其功能是在字符串列表中检索符合拼音索引字符串的所有字符串。可将其添加到窗体单元文件的implementation部分。
 程序代码
使用拼音首字母序列实现检索功能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;
使用拼音首字母序列实现检索功能


上面程序中引用的GetPYIndexChar函数用于获取指定汉字的拼音索引字母,如“杜”的索引字母是“D”。该函数的代码如下所示,也将其添加到窗体单元文件的implementation部分。
 程序代码
使用拼音首字母序列实现检索功能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;

完成之后,按下F9键运行程序,用户在编辑框中输入人名的拼音首字母后,在ListBox2中会自动检索对应的姓名。

相关文章:

  • 2022-12-23
  • 2022-01-28
  • 2022-12-23
  • 2021-08-05
  • 2021-12-15
  • 2021-07-09
  • 2021-11-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
相关资源
相似解决方案