類(lèi)似qq的漢字拼音首字查詢(xún)
項(xiàng)目里要用到這個(gè)功能,自己邊琢磨邊找資料,弄出一個(gè)demo,大概思路是這樣:用Properties的load()來(lái)加載一個(gè)固定格式的文本文件注1,然后直接當(dāng)Hashtable用。
package org.navyblue.tests;

import java.util.*;


/**//**
*
* 類(lèi)似qq的漢字拼音首字查詢(xún)方法
* by zqc 2005-08-04
*
***/
public class pyTest


{

public pyTest()
{}

public static void main(String[] args)throws Exception
{
Properties pyMap = new Properties();
//拼音隱射表
pyMap.load(new java.io.FileInputStream("pyutf.txt"));
//測(cè)試字符

String[] testStr =
{"我們","阿哈","嘎嘎","干什么","鄭晴川","誰(shuí)"};
//模擬輸入
String input = "g";
System.out.println ("輸入查詢(xún)字符:g");
Vector inis = new Vector();
Vector results = new Vector();
Vector results2 = new Vector();
//返回所有測(cè)試字符串的首字的打頭拼音

for( int i =0 ; i < testStr.length ; i++ )
{

String ini =new String( new char[]
{ testStr[i].charAt(0)} );
String k = (String)pyMap.get(ini);
results.add(k);
results2.add(testStr[i]);
}
//輸出結(jié)果
int l = results.size();

for(int i = 0 ; i < l ; i++)
{
String c = (String)results.get(i);

if(c.equals(input))
{
System.out.println (results2.get(i));
}
}
}
}
----
注1:這個(gè)映射文件系網(wǎng)上找來(lái)的,大約有14K的漢字量,一般也夠用了~用文本工具對(duì)原先的格式做了些修改使它符合Properties的格式,然后再用native2ascii轉(zhuǎn)成utf-8編碼。
2005年8月8日 0:48:54































































----
注1:這個(gè)映射文件系網(wǎng)上找來(lái)的,大約有14K的漢字量,一般也夠用了~用文本工具對(duì)原先的格式做了些修改使它符合Properties的格式,然后再用native2ascii轉(zhuǎn)成utf-8編碼。
2005年8月8日 0:48:54
posted on 2005-08-08 00:59 where the amazing happens 閱讀(1072) 評(píng)論(4) 編輯 收藏 所屬分類(lèi): 一般應(yīng)用