在我们的系统中,可能经常需要按首字母排序一些信息(比如淘宝商城的品牌列表字母序排列),那么我们就需要一个能够根据汉字查询对应的拼音,取出拼音的首字母即可。
我们使用sourceforge.pinyin4j开源包来完成我们的功能。
使用很简单:
提供的工具类是下面这个PinyinHelper.java help类,里面有所有开放的API,有几个方法是对应转换成不同的拼音系统
[java]
/**
* This file is part of pinyin4j (http://sourceforge.net/projects/pinyin4j/)
* and distributed under GNU GENERAL PUBLIC LICENSE (GPL).
*
* pinyin4j is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* pinyin4j is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with pinyin4j.
*/
package net.sourceforge.pinyin4j;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* A class provides several utility functions to convert Chinese characters
* (both Simplified and Tranditional) into various Chinese Romanization
* representations
*
* @author Li Min (xmlerlimin@gmail.com)
*/
public class PinyinHelper
{
/**
* Get all unformmatted Hanyu Pinyin presentations of a single Chinese
* character (both Simplified and Tranditional)
*
*
* For example,
If the input is '间', the return will be an array with
* two Hanyu Pinyin strings:
"jian1"
"jian4"
If the
* input is '李', the return will be an array with single Hanyu Pinyin
* string:
"li3"
*
* Special Note: If the return is "none0", that means the input
* Chinese character exists in Unicode CJK talbe, however, it has no
* pronounciation in Chinese
*
* @param ch
* the given Chinese character
*
* @return a String array contains all unformmatted Hanyu Pinyin
* presentations with tone numbers; null for non-Chinese character
*
*/
static public String[] toHanyuPinyinStringArray(char ch)
{
return getUnformattedHanyuPinyinStringArray(ch);
}
/**
* Get all Hanyu Pinyin presentations of a single Chinese character (both
* Simplified and Tranditional)
*
*
* For example,
If the input is '间', the return will be an array with
* two Hanyu Pinyin strings:
"jian1"
"jian4"
If the
* input is '李', the return will be an array with single Hanyu Pinyin
* string:
"li3"
*
*
* Special Note: If the return is "none0", that means the input
* Chinese character is in Unicode CJK talbe, however, it has no
* pronounciation in Chinese
*
* @param ch
* the given Chinese character
* @param outputFormat
* describes the desired format of returned Hanyu Pinyin String
*
* @return a String array contains all Hanyu Pinyin presentations with tone
* numbers; return null for non-Chinese character
*
* @throws BadHanyuPinyinOutputFormatCombination
* if certain combination of output formats happens
*
* @see HanyuPinyinOutputFormat
* @see BadHanyuPinyinOutputFormatCombination
*
*/
static public String[] toHanyuPinyinStringArray(char ch,