把中文变成zhongwen

简介: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.
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;

public class Test {

	//用于存放输出样式的
	private HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
	
	public Test() {
		outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//大小写的问题
		outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);//关于v的显示方式
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//关于音标的显示
	}
	
	public static void  main(String args[]) throws BadHanyuPinyinOutputFormatCombination {
		Test test = new Test();
		String result = test.convertToPinYin("中文aaaaabbb.....',;12.3");
		System.out.println(result);
	}
	
	public String convertToPinYin(String str) throws BadHanyuPinyinOutputFormatCombination {
		char[] chars = str.toCharArray();
		StringBuffer result = new StringBuffer();
		for (char c:chars) {
			if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {//表示匹配中文
				//下面的results是表示该中文字所有的拼音,如“中”有"中文”、"相中"两种读法,所以当只需要拼音而不需要知道读第几声时,只需取第一个,多音字需慎重
				String results[] = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
				result.append(results[0]);
				//字母和数字直接显示
			} else if (Character.isLetter(c)|| Character.isDigit(c)){
				result.append(c);
			}
		}
		return result.toString();
	}
}
目录
相关文章
itextpdf 中文不显示问题
# 现象 itextpdf 打印时中文字体显示不出来,莫名其妙的消失不见了。具体现象如下图所示。 ![](https://ata2-img.oss-cn-zhangjiakou.aliyuncs.com/neweditor/ddc69588-4fc6-46ff-9d33-07f99340c963.png) 真正的理想情况如下图。 ![](https://ata2-img.oss-cn-zhangj
itextpdf 中文不显示问题
|
10月前
中文转拼音
中文转拼音
112 0
|
人工智能 安全 程序员
谁说不能用中文写代码?
现代计算机和编程的起源和推动力量主要源自美国,再加上26个字母很便于表示(算上大小写,6位bit就够了),因此英语一直是编程领域的不二之选。但这就给部分非英语国家的编程学习者带来一些困扰。以至于有些人还没开始学,就担心自己的英语问题。这完全没必要,因为编程初期所用到的单词很有限,你就当做一个符号去记,也能很快熟悉。而且我们一直在讲的 Python,也可以“用中文”来编程。
/u5f20 之类的转中文
/u5f20 之类的转中文
376 0
|
JavaScript
unicode转中文的工具
unicode转中文的工具
321 0
unicode转中文的工具
|
缓存 人工智能 数据可视化
中文问题解决| 学习笔记
快速学习中文问题解决
108 0
|
前端开发
前端脚本!网站图片素材中文转英文
写网页的时候, 我们经常需要使用图片素材, 图片素材如果是中文名, 挂到服务器会会引发乱码, 我们需要将图片名称改为英文字符才可以使用 而起名是一个世界级难题, 为图片素材起英文名更是一件极其蛋疼的事 有些人英语不好, 直接用拼音命名, 而拼音闹出的笑话更是无法计量.
1114 0