android开发中使用pinyin4android转换汉字为pinying

public class Chinese2SpellUtils {
	
	 /**
     * 使用pinyin4android.jar将汉字转换为拼音
     * https://code.google.com/p/pinyin4android/downloads/detail?name=pinyin4android1.0.jar&can=2&q=
     * @param chineseStr
     * @return
     */
    public static String chineneToSpell(Context context,String chineseStr) {
        if (isChinese(chineseStr)) {
            String pinying = PinyinUtil.toPinyin(context,chineseStr);
            return pinying.toUpperCase();
        }
        return chineseStr;
    }

    /**
     * 判断字符串中是否包含有中文
     * 
     * @param str
     * @return
     */
    public static boolean isChinese(String str) {
        String regex = "[\\u4e00-\\u9fa5]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }

    /**
     * 判断是否是a-z
     * 
     * @param str
     * @return
     */
    public  static boolean isA2Z(String str) {
        String regex = "[A-Za-z]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }

    //获得汉语拼音首字母  
    public static String getAlpha(String spellStr) {    
        if (spellStr == null) {    
            return "#";    
        }    
    
        if (spellStr.trim().length() == 0) {    
            return "#";    
        }    
    
        char c = spellStr.trim().substring(0, 1).charAt(0);    
        // 正则表达式,判断首字母是否是英文字母    
        Pattern pattern = Pattern.compile("^[A-Za-z]{1}");    
        if (pattern.matcher(c + "").matches()) {    
            return (c + "").toUpperCase();    
        } else {    
            return "#";    
        }    
    }    

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值