4
hao
2025-04-16 c5fb1fbcbb2bf4d511773d348f9ef625855c61fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.utils;
 
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 
/**
 * 汉字拼音转换
 */
public class PinYinUtil {
 
    /**
     * 汉字转为拼音首字母大写
     * 注意:汉字会取首字母,英文字母保持不变,数字和其他符号忽略
     * @param chinese
     * @return
     */
    public static String toFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();//转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++){
            if (newChar[i] > 128){
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (Exception e) {
                    //转换错误跳过
                    continue;
                }
            }else if ((newChar[i] >= 65 && newChar[i] <= 90) || (newChar[i] >= 97 && newChar[i] <= 122)){
                pinyinStr += newChar[i];
            }else{
                continue;
            }
        }
 
        //转换成大写字母
        if(pinyinStr.length() > 0){
            pinyinStr = pinyinStr.toUpperCase();
        }
 
        return pinyinStr;
    }
 
    /**
     * 公司名称去除“有限责任公司”或者“有限公司”或者“公司”
     * @param companyName
     * @return
     */
    public static String subCompanyName(String companyName){
        if(companyName.contains("有限责任公司")){
            companyName = companyName.replace("有限责任公司", "");
        }
        if(companyName.contains("有限公司")){
            companyName = companyName.replace("有限公司", "");
        }
        if(companyName.contains("公司")){
            companyName = companyName.replace("公司", "");
        }
        return companyName;
    }
}