zoukankan      html  css  js  c++  java
  • pinyin4j工具类--把汉字转为拼音的插件

    package com.xxx.xxx.xxx;
    
    import net.sourceforge.pinyin4j.PinyinHelper;
    import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * Created by 13375 on 2019/12/9 15:28
     *
     * @ClassName: com.jxdinfo.hussar.common
     * @Description: 1
     * @author: lifujian
     * @date: 2019/12/9  15:28
     */
    public class ChineseToPinyinUtils {
    
    	public static enum Type {
    		UPPERCASE,              //全部大写
    		LOWERCASE,              //全部小写
    		FIRSTUPPER              //首字母大写
    	}
    
    	public static String toPinYin(String str){
    		return toPinYin(str, "", Type.UPPERCASE);
    	}
    
    
    	public static String toPinYin(String str,String spera){
    		return toPinYin(str, spera, Type.UPPERCASE);
    	}
    
    
    	/**
    
          * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
    
          * 如: 明天 转换成 MINGTIAN
    
          * @param str:要转化的汉字
    
          * @param spera:转化结果的分割符
    
          * @return
    
          * @throws BadHanyuPinyinOutputFormatCombination
    
          */
    
    	public static String toPinYin(String str, String spera, Type type){
    		try {
    			HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    			if(str == null || str.trim().length()==0)
    				return "";
    			if(type == Type.UPPERCASE)
    				format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    			else
    				format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    			String py = "";
    			String temp = "";
    			String[] t;
    			for(int i=0;i<str.length();i++){
    				char c = str.charAt(i);
    				if((int)c <= 128)
    					py += c;
    				else{
    					t = PinyinHelper.toHanyuPinyinStringArray(c, format);
    					if(t == null)
    						py += c;
    					else{
    						temp = t[0];
    						if(type == Type.FIRSTUPPER)
    							temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
    						String pattern = "[\d]{1}";//正则去掉音调即数字
    						temp = temp.replaceAll(pattern,"");
    						py += temp+(i==str.length()-1?"":spera);
    					}
    				}
    			}
    			return py.trim();
    		}catch (BadHanyuPinyinOutputFormatCombination e){
    			e.printStackTrace();
    			return null;
    		}
    	}
    }
    

      POM文件引入:

    <!-- pinyin4j 将汉字转为拼音  -->
    <dependency>
        <groupId>com.belerweb</groupId>
        <artifactId>pinyin4j</artifactId>
        <version>2.5.0</version>
    </dependency>
  • 相关阅读:
    攻防世界新手练习题_MOBILE(移动)
    攻防世界新手练习题_CRYPTO(加密)
    攻防世界新手练习题_REVERSE(逆向)
    攻防世界新手练习题_PWN(漏洞利用)
    攻防世界新手练习题_WEB(渗透)
    攻防世界新手练习题_MISC(杂项)
    数据传输流程图
    leetcode hot 100- 62. 不同路径
    leetcode hot 100
    leetcode hot 100
  • 原文地址:https://www.cnblogs.com/Sword007/p/12016885.html
Copyright © 2011-2022 走看看