fix:获取拼音

上级 5bd6820f
......@@ -7,7 +7,6 @@ import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* 拼音工具类
*
......@@ -18,7 +17,7 @@ import java.util.List;
public class PinyinUtil {
/**
* 获取中文完整拼音
* 获取中文完整拼音-不带音标
*
* @param chineseStr
* @return
......@@ -32,6 +31,41 @@ public class PinyinUtil {
return stringBuilder.toString();
}
/**
* 获取中文完整拼音-指定分隔符
*
* @param chineseStr
* @return
*/
public static String getPinyin(String chineseStr, String separator) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin(chineseStr);
StringBuilder stringBuilder = new StringBuilder();
if (!CollectionUtils.isEmpty(pinyins)) {
final int size = pinyins.size();
for (int i = 0; i < size; i++) {
if (i != 0 && i != size) {
stringBuilder.append(separator);
}
stringBuilder.append(pinyins.get(i).getPinyinWithoutTone());
}
}
return stringBuilder.toString();
}
/**
* 带音标
*
* @param chineseStr
* @return
*/
public static String getPinyinWithToneMark(String chineseStr) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin(chineseStr);
StringBuilder stringBuilder = new StringBuilder();
for (Pinyin pinyin : pinyins) {
stringBuilder.append(pinyin.getPinyinWithToneMark());
}
return stringBuilder.toString();
}
/**
* 获取中文拼音首字母
......@@ -46,4 +80,9 @@ public class PinyinUtil {
}
return String.valueOf(pinyins.get(0).getPinyinWithoutTone().charAt(0));
}
public static void main(String[] args) {
System.out.println(PinyinUtil.getPinyin("秦英杰", "_"));
System.out.println(PinyinUtil.getInitial("秦英杰"));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册