当前位置: 首页 > 工具软件 > Pinyin4Net > 使用案例 >

JAVA中的pinyin4j,java开源类库pinyin4j的使用

柳钟展
2023-12-01

import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;

public class PinyinComparator implements Comparator {

private String concatPinyinStringArray(String[] pinyinArray) {

StringBuffer pinyinStrBuf = new StringBuffer();

if ((null != pinyinArray) && (pinyinArray.length > 0)) {

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

pinyinStrBuf.append(pinyinArray[i]);

}

}

String outputString = pinyinStrBuf.toString();

return outputString;

}

public int compare(Object o1, Object o2) {

int length1 = ((String) o1).length();

int length2 = ((String) o2).length();

int length = length1 - length2 > 0 ? length2 : length1;

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

char c1 = ((String) o1).charAt(i);

char c2 = ((String) o2).charAt(i);

String c1topinyin = concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray(c1));

String c2topinyin = concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray(c2));

&nbs

 类似资料: