maven依赖
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; /** * * @author InJavaWeTrust * */ public class GetPinYin { /** * 得到 全拼 * @param hanyu * @return 全拼 */ public static String getPinYin(String hanyu) { char[] array = hanyu.toCharArray(); String[] pinyin = new String[array.length]; //创建格式化对象 HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); //转换后以全小写方式输出 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //无声调 format.setVCharType(HanyuPinyinVCharType.WITH_V); //以v表示u String quanpin = ""; try { for (int i = 0; i < array.length; i++) { // 判断是否为汉字字符 if (Character.toString(array[i]).matches("[\\u4E00-\\u9FA5]+")) { pinyin = PinyinHelper.toHanyuPinyinStringArray(array[i], format); quanpin += pinyin[0]; } else { quanpin += Character.toString(array[i]); } } return quanpin; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return quanpin; } /** * 得到中文首字母 * @param hanyu * @return 中文首字母 */ public static String getPinYinHeadChar(String hanyu) { String headChar = ""; for (int i = 0; i < hanyu.length(); i++) { char hanzi = hanyu.charAt(i); String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi); if (pinyinArray != null) { headChar += pinyinArray[0].charAt(0); } else { headChar += hanzi; } } return headChar; } public static void main(String[] args) { String hanyu = "鱼驴中華人民共和國1q"; System.out.println(getPinYin(hanyu)); System.out.println(getPinYinHeadChar(hanyu)); } }

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

JDBC...

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

How to stop subsequent code execution when ID is repeated in Java programming. When learning Java programming, you often encounter such a requirement: when a certain condition is met,...

In-depth discussion of final consistency: In the distributed system of application scenarios and implementation methods, ensuring data consistency has always been a major challenge for developers. This article...

The troubleshooting idea of SSH connection failure after SpringBoot service has been running for a period of time has recently encountered a problem: a Spring...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.