取汉字拼音首字母--生成不重复ID(汉字--拼音--首字母)
生活随笔
收集整理的這篇文章主要介紹了
取汉字拼音首字母--生成不重复ID(汉字--拼音--首字母)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工作上需要生成一些不重復的Id,并且Id是固定前綴加客戶拼音首字母相連產生的,如有重復,末尾加數字
首先是漢字轉拼音的jar包
public String getUniqueAk(String cust_name){int index=0;String value = "";while(true){ StringBuffer sb = new StringBuffer("a.c.l.");sb.append(cn2FirstSpell(cust_name));if(index != 0){sb.append(index);}index++;String str = sb.toString();A a = aService.get(str);//是否重復if(aService.get(str) == null){value = str;break;//不重復就跳出}}return value;}
public String cn2FirstSpell(String chinese) {StringBuffer pybf = new StringBuffer();char[] arr = chinese.toCharArray();HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < arr.length; i++) {if (arr[i] > 128) {try {String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);if (_t != null) {pybf.append(_t[0].charAt(0));}} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {pybf.append(arr[i]);}}return pybf.toString().replaceAll("\\W", "").trim(); }
總結
以上是生活随笔為你收集整理的取汉字拼音首字母--生成不重复ID(汉字--拼音--首字母)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: indesign怎么查找拼音_用Inde
- 下一篇: 网络营销与html,网络营销和线上推广的