Convert Chinese characters to pinyin using pinyin4j

Source: Internet
Author: User

MAVEN introduces dependencies:

<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> < Version>2.5.0</version></dependency>

It encapsulates a tool class:

package com.ylmob.yunpay.util.pinyin;import java.util.regex.matcher;import  java.util.regex.pattern;import net.sourceforge.pinyin4j.pinyinhelper;import  net.sourceforge.pinyin4j.format.hanyupinyinoutputformat;import  net.sourceforge.pinyin4j.format.hanyupinyintonetype;import  net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatcombination;import org.slf4j.logger; import org.slf4j.loggerfactory;/** *  @author  DJM */public class  hanyupinyinutil {        logger logger =  Loggerfactory.getlogger (Hanyupinyinutil.class);        private  Hanyupinyinutil () {    }        public static  hanyupinyinutil getinstance () {        return new  Hanyupinyinutil ();     }  &Nbsp;     public string topinyin (String chinesecharacter) {         String pinyin =  "";         for (Character chara: chinesecharacter.tochararray ()) {             logger.info ("the character is {}.",  chara);             Pattern pt =  Pattern.compile ("[\\u4e00-\\u9fa5]+");             Matcher mt = pt.matcher (Chara.tostring ());             if (Mt.matches ()) {                 logger.info ("The character {} is match.",  chara);                 HanyuPinyinOutputFormat format = new  Hanyupinyinoutputformat ();                 format.settonetype (Hanyupinyintonetype.without_tone);                 String[] hyArr = null;                 try {                     hyarr  = pinyinhelper.tohanyupinyinstringarray (Chara, format);                 } catch  ( Badhanyupinyinoutputformatcombination e)  {                    &nbsP;e.printstacktrace ();                 }                 Pinyin = pinyin.concat (hyarr[0]);             }else{                // Filter whitespace characters                 pattern  blankpt = pattern.compile ("\\s+");                  matcher blankmt = blankpt.matcher (Chara.toString ());                 if (! Blankmt.matches ()) {                     p Inyin = pinyin.concat (chara.tostring ());                 }             }        }        return  pinyin;    }    }


Convert Chinese characters to pinyin using pinyin4j

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.