类似qq的汉字拼音首字查询

项目里要用到这个功能,自己边琢磨边找资料,弄出一个demo,大概思路是这样:用Properties的load()来加载一个固 定格式的文本文件 注1,然后直接当Hashtable用。
None.gif package org.navyblue.tests;
None.gif
None.gifimport java.util.
* ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**
InBlock.gif *
InBlock.gif * 类似qq的汉字拼音首字查询方法
InBlock.gif * by zqc 2005-08-04
InBlock.gif *
ExpandedBlockEnd.gif **
*/

None.gif
public   class  pyTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public pyTest()dot.gif{}
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args)throws Exceptiondot.gif{
InBlock.gif        Properties pyMap 
= new Properties();
InBlock.gif        
//拼音隐射表
InBlock.gif
        pyMap.load(new java.io.FileInputStream("pyutf.txt"));
InBlock.gif        
//测试字符
ExpandedSubBlockStart.gifContractedSubBlock.gif
        String[] testStr = dot.gif{"我们","阿哈","嘎嘎","干什么","郑晴川",""};
InBlock.gif        
//模拟输入
InBlock.gif
        String input = "g";
InBlock.gif        
InBlock.gif        System.
out.println ("输入查询字符:g");
InBlock.gif        Vector inis 
= new Vector();
InBlock.gif        Vector results 
= new Vector();
InBlock.gif        Vector results2 
= new Vector();
InBlock.gif        
InBlock.gif        
//返回所有测试字符串的首字的打头拼音
ExpandedSubBlockStart.gifContractedSubBlock.gif
        forint i =0 ; i < testStr.length ; i++ )dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            String ini 
=new String( new char[] dot.gif{ testStr[i].charAt(0)} );
InBlock.gif            String k 
= (String)pyMap.get(ini);
InBlock.gif            results.add(k);
InBlock.gif            results2.add(testStr[i]);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
//输出结果
InBlock.gif
        int l = results.size();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i = 0 ; i < l ; i++)dot.gif{
InBlock.gif            String c 
= (String)results.get(i);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(c.equals(input))dot.gif{
InBlock.gif                System.
out.println (results2.get(i));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


----
注1:这个映射文件系网上找来的,大约有14K的汉字量,一般也够用了~用文本工具对原先的格式做了些修改使它符合Properties的格式,然后再用native2ascii转成utf-8编码。



2005年8月8日 0:48:54
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值