JAVA面向对象编程艺术与思想:中文转拼音1
需求:把名字变成拼音,然后用空格分割每个字
我学生用面向过程的思想来编实现这个类,我把他的代码稍微整理了下,下面是他的代码:
pom.xml
先要去导入一个三方jar,用于转化拼音
<dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>
PinyinTool
package org.chb.test;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.exception.BadHanyuPinyinOutputFormatCombination;/*** * Created by 33css*/
public class PinyinTool {HanyuPinyinOutputFormat format = null;public PinyinTool() {format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.UPPERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);}/*** 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 如: 明天 转换成 MINGTIAN* * @param content* @return* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYin(String content, HanyuPinyinOutputFormat format)throws BadHanyuPinyinOutputFormatCombination {if (content == null || content.trim().length() == 0)return "";StringBuffer result = new StringBuffer();for (int i = 0; i < content.length(); i++) {char c = content.charAt(i);result.append(getPinyin(c, format)).append(" ");}return result.toString();}/*** 把传入的汉字变成拼音* @param content 汉字内容* @param format 拼音格式* @return*/private String getPinyin(char content, HanyuPinyinOutputFormat format) {if ((int) content <= 128) {//如果小于128 说明不是中文汉字 则直接返回return String.valueOf(content);} else {try {return PinyinHelper.toHanyuPinyinStringArray(content, format)[0];//toHanyuPinyinStringArray多音字 会获取到数组 使用第一个字} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}}return null;}
}
test测试该功能
package org.chb.test;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;public class Test {public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {PinyinTool tool = new PinyinTool();System.out.println(tool.toPinYin("罗若峰", createFormat()));}/*** 创建样式* @return*/public static HanyuPinyinOutputFormat createFormat() {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.LOWERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);return format;}
}
![](/assets/blank.gif)
/*** 把名字放到姓的前面* @param content 内容* @return 返回名字在前的字段,如:若峰罗*/private String sortContent(String content) {return content.substring(1)+content.substring(0,1);}
/*** 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 如: 明天 转换成 MINGTIAN* * @param content* @return* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYin(String content, HanyuPinyinOutputFormat format)throws BadHanyuPinyinOutputFormatCombination {if (content == null || content.trim().length() == 0)return "";StringBuffer result = new StringBuffer();content = sortContent(content);//修改名和姓的顺序for (int i = 0; i < content.length(); i++) {char c = content.charAt(i);result.append(getPinyin(c, format)).append(" ");}return result.toString();}
JAVA面向对象编程艺术与思想:中文转拼音1相关推荐
- JAVA面向对象编程艺术与思想:中文转拼音2
我现在用面向对象的思想来实现名字的转换 把名字中每个字都看成一个对象,建立类PinyinChar 该类包括字和拼音对象属性 package org.lrf.pinyin;import net.sour ...
- java面向对象编程思想的理解
1.我们总说java是一门面向对象编程的语言,那什么是面向对象呢? 我是这样理解的,对象是事物存在的实体,如,猪.狗,花早等都是对象,对象由两部分组成.面向对象编程的三大特点:继承,多态,类是封装对象 ...
- java面向对象编程的思想_java面向对象编程思想的理解
1.我们总说java是一门面向对象编程的语言,那什么是面向对象呢? 我是这样理解的,对象是事物存在的实体,如,猪.狗,花早等都是对象,对象由两部分组成.面向对象编程的三大特点:继承,多态,类是封装对象 ...
- java面向对象编程思想_Java面向对象编程思想的理解
1.我们总说java是一门面向对象编程的语言,那什么是面向对象呢? 我是这样理解的,对象是事物存在的实体,如,猪.狗,花早等都是对象,对象由两部分组成.面向对象编程的三大特点:继承,多态,类是封装对象 ...
- 孙卫琴:我为什么要写《Java面向对象编程》
孙卫琴:我为什么要写<Java面向对象编程> 特约作者:孙卫琴 策划 & 设计 & 制作:李大微 当<精通Struts>和<精通Hibernate> ...
- java面向对象编程基础
java面向对象编程基础 前言:什么是java 是咖啡飘香的清晨 - 是斯坦福校园意浓情深 - 是James的思想睿智 是剁手党双十一挥舞的利刃 是大数据云计算驰骋的平台 - 是ATM上吐出的钞票 - ...
- 八、Java面向对象编程(类、对象、方法、重载、可变参数、作用域、构造器、this本质)
文章目录 Java面向对象编程(类.对象.方法.重载.可变参数.作用域.构造器.this本质) 一.类与对象 1. 类与对象的引出 2. 使用现有技术解决 3. 现有技术解决的缺点分析 4. 类与对象 ...
- java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super
目录点击跳转 包 包的命名方法 导入包中的类 系统包的介绍 **注意事项** 继承 基础知识 构造方法 **基础语法** `protected`修饰符 组合 `this`和`super`关键字 `th ...
- java实验Java面向对象编程_Java实验项目 面向对象编程.doc
Java实验项目 面向对象编程 Java实验项目二 面向对象编程 第1部分 类与对象 [实验目的] 熟悉Java面向对象程序设计的基本思想. 掌握类与对象的定义及使用方法. 掌握package语句与i ...
最新文章
- CCTouchDispatcher sharedDispatcher 方法过期
- OpenCV中的透视变换介绍
- Arduino可穿戴开发入门教程Arduino开发环境介绍
- 数据结构第二版之(课后题)BF算法病毒感染检测
- 【XML DOM】解析XML Dom
- activeMQ的三种通讯模式
- remote: Incorrect username or password ( access token ) fatal: Authentication failed for gitee
- celery AttributeError: 'str' object has no attribute 'items'
- 思维导图学Java编程思想
- php微信段子,年度挤进前十名的微信段子,笑死了
- Android Studio3.2经常用的一些依赖(以后再加)
- The Clean Architecture--一篇很不错的关于架构的文章
- python collections,函数等笔记
- 【无标题】IDM + 油猴 + 百度云
- libiconv android编译,NDK编译经常使用开源库-libiconv
- 深入了解KNN算法原理
- IaaS PaaS SaaS DaaS基础设施即服务、平台即服务、软件即服务、数据即服务详解
- C++Primer 第9章 顺序容器
- 七日杀服务器无限刷空投,七日杀空投作弊代码 | 手游网游页游攻略大全
- jQuery 操作整理
热门文章
- ubuntu下面玩街机+北通野牛手柄
- qemu下的USB直通功能介绍
- jacob电脑重启后word/excel转pdf是0KB问题解决方法
- Day12 instanceof 内部类
- 可爱的头像【 InsCode Stable Diffusion 美图活动一期】
- 微信公众号简单开发(10)链接消息
- 硬盘容量计算公式为什么是这个【硬盘容量 = 磁柱数量 * 磁头数量 * 扇区数量 * 512Byte】
- 利用微搭实现下拉框动态填充值得问题
- (36)RuntimeError: Given groups=4, weight of size [4, 1, 11, 11], expected input xxxxxxxxx
- 2022电赛小车跟随行驶系统(C题)复盘