소스 검색

通用组件

Progr1mmer 6 년 전
부모
커밋
acf9ef684d
3개의 변경된 파일46개의 추가작업 그리고 0개의 파일을 삭제
  1. 6 0
      commons/utils/pom.xml
  2. 32 0
      commons/utils/src/main/java/com/yihu/utils/pinyin/PinyinUtil.java
  3. 8 0
      pom.xml

+ 6 - 0
commons/utils/pom.xml

@ -72,6 +72,12 @@
            <artifactId>commons-collections</artifactId>
        </dependency>
        <!-- 拼音 -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
        </dependency>
    </dependencies>
</project>

+ 32 - 0
commons/utils/src/main/java/com/yihu/utils/pinyin/PinyinUtil.java

@ -0,0 +1,32 @@
package com.yihu.utils.pinyin;
import net.sourceforge.pinyin4j.PinyinHelper;
/**
 * 拼音工具包装。
 *
 * @author Sand
 * @version 1.0
 * @created 2015.07.30 15:33
 */
public class PinyinUtil {
    /**
     * 提取汉字的首字母
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str, boolean upperCase) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return upperCase ? convert.toUpperCase() : convert;
    }
}

+ 8 - 0
pom.xml

@ -59,6 +59,7 @@
        <version.bcprov-jdk15on>1.59</version.bcprov-jdk15on>
        <version.json>20160212</version.json>
        <version.json-lib>2.4</version.json-lib>
        <version.pinyin4j>2.5.0</version.pinyin4j>
    </properties>
    <dependencyManagement>
@ -243,6 +244,13 @@
                <version>${version.bcprov-jdk15on}</version>
            </dependency>
            <!-- 拼音 -->
            <dependency>
                <groupId>com.belerweb</groupId>
                <artifactId>pinyin4j</artifactId>
                <version>${version.pinyin4j}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>