汉字转拼音首字母的java实现
工作中经常会遇到的一些排序问题,比如 按汉字的拼音首字母排序,比如人名排序等,就要用到下面的方法了,思路:
1. 获得汉字
2. 将汉字转换成首字母,并记录下(必要时保存到数据库)
3. 按首字母进行排序并展示
控制台输出演示:
汉字转首字母演示,请输入汉字: 4654^*&^&*^_FDSF你i好3啊>?>?>*(*33P{}{ 直接转换convertTo: 4654^*&^&*^_FDSFnih3a>?>?>*(*33P{}{
保留一些字符convertAndClear: 4654^&^&^_FDSFnih3a>?>?>(33P{}{
仅保留字母和数字convertAndClearAll: 4654FDSFnih3a33P
代码如下,直接复制后就能使用:
package pinyin; import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("汉字转首字母演示,请输入汉字:");
String str = sc.next();
String py1 = ChineseToFirstCharUtil.convertTo(str);
String py2 = ChineseToFirstCharUtil.convertAndClear(str);
String py3 = ChineseToFirstCharUtil.convertAndClearAll(str);
System.out.print("\r\n直接转换convertTo:" + py1);
System.out.print("\r\n保留一些字符convertAndClear:" + py2);
System.out.print("\r\n仅保留字母和数字convertAndClearAll:" + py3);
}
}
package pinyin; /**
* 汉字转首字母缩写
*
* @author 张云飞vir基于网上获得的资料的修改2015-3-3 http://home.cnblogs.com/u/vir56k/
*
*/
public class ChineseToFirstCharUtil {
/**
* 汉字转拼音缩写
*
* @param str
* 要转换的汉字字符串
* @return String 拼音缩写
*/
public static String convertTo(String str) {
String tempStr = "";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 33 && c <= 126) {// 字母和符号原样保留
tempStr += String.valueOf(c);
} else {// 累加拼音声母
tempStr += getPYChar(String.valueOf(c));
}
}
return tempStr;
} /**
* 汉字转拼音缩写,清理无效字符(保留一些键盘字符)
* @param str 汉字
* @return String 缩写
*/
public static String convertAndClear(String str) {
return convertTo(str).replace("*", "");
} /**
* 汉字转拼音缩写,清理无效字符(清理任何非数字和字母)
* @param str 汉字
* @return String 缩写
*/
public static String convertAndClearAll(String str) {
String str1 = convertTo(str).replace("*", "");
StringBuilder sb = new StringBuilder();
for(char c : str1.toCharArray()){
if((c>=48 && c<= 57) || (c>=65 && c<= 90) || (c>=97 && c<= 122)){
sb.append(c);
}
}
return sb.toString();
} /**
* 取单个字符的拼音声母
*
* @param c
* //要转换的单个汉字
* @return String 拼音声母
*/
private static String getPYChar(String c) {
byte[] array = new byte[2];
array = String.valueOf(c).getBytes();
int i = (short) (array[0] - '\0' + 256) * 256
+ ((short) (array[1] - '\0' + 256));
if (i < 0xB0A1)
return "*";
if (i < 0xB0C5)
return "a";
if (i < 0xB2C1)
return "b";
if (i < 0xB4EE)
return "c";
if (i < 0xB6EA)
return "d";
if (i < 0xB7A2)
return "e";
if (i < 0xB8C1)
return "f";
if (i < 0xB9FE)
return "g";
if (i < 0xBBF7)
return "h";
if (i < 0xBFA6)
return "j";
if (i < 0xC0AC)
return "k";
if (i < 0xC2E8)
return "l";
if (i < 0xC4C3)
return "m";
if (i < 0xC5B6)
return "n";
if (i < 0xC5BE)
return "o";
if (i < 0xC6DA)
return "p";
if (i < 0xC8BB)
return "q";
if (i < 0xC8F6)
return "r";
if (i < 0xCBFA)
return "s";
if (i < 0xCDDA)
return "t";
if (i < 0xCEF4)
return "w";
if (i < 0xD1B9)
return "x";
if (i < 0xD4D1)
return "y";
if (i < 0xD7FA)
return "z";
return "*";
}
}
汉字转拼音首字母的java实现的更多相关文章
- (转载)delphi中获取汉字的拼音首字母
delphi中获取汉字的拼音首字母1.py: array[216..247] of string = ({216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + ' ...
- SqlServer 汉字转换拼音首字母函数
CREATE function [dbo].[Func_GetPY](@str nvarchar(4000))returns nvarchar(4000)asbegin set @str=RTRIM( ...
- 简单测试--C#实现中文汉字转拼音首字母
第一种: 这个是自己写的比较简单的实现方法,要做汉字转拼音首字母,首先应该有一个存储首字母的数组,然后将要转拼音码的汉字与每个首字母开头的第一个汉字即“最小”的汉字作比较,这里的最小指的是按拼音规则比 ...
- sql获取汉字的拼音首字母的函数
ql获取汉字的拼音首字母 if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...
- C#获取包括一二级汉字的拼音 首字母
C#获取包括一二级汉字的拼音 首字母 声母 汉字拼音转换 using System; using System.Collections.Generic; using System.Linq; usin ...
- js汉字转拼音首字母
js汉字转拼音首字母 2018-04-09 阅读 1018 收藏 1 原链:segmentfault.com 分享到: 前端必备图书<JavaScript设计模式与开发实践> > ...
- C# 获取汉字的拼音首字母和全拼(含源码)
C# 获取汉字的拼音首字母 一种是把所有中文字符集合起来组成一个对照表:另一种是依照汉字在Unicode编码表中的排序来确定拼音的首字母.碰到多音字时就以常用的为准(第一种方法中可以自行更改,方法为手 ...
- 获取汉字的拼音首字母--pinyin
var pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, options = { checkPo ...
- java获取多个汉字的拼音首字母
本文属于http://java.chinaitlab.com/base/803353.html原创!!! public class PinYin2Abbreviation { // 简体中文的编码范围 ...
随机推荐
- [转]IDEA 出现编译错误 Multi-catches are not supported a this language level 解决方法
转自 http://blog.csdn.net/qq465235530/article/details/53897538 首先出现这种问题是说明正在使用低版本jdk编译其本身不支持的语法,出现这种情况 ...
- xbox360 双65厚机自制系统无硬盘 U盘玩游戏方法
因为没有硬盘,又没有光盘.所以想把游戏放在U盘里面.用U盘来做为硬盘玩游戏. 现有的自制系统主要是FSD,但是FSD要用硬盘才能安装,理论上U盘也可以,但是我没有尝试了. 这里介绍的是玩xex格式的游 ...
- unity, ugui toggle, dynamic bool
假设Canvas_debugControl.cs有一个函数 public void showNextSceneButton(bool value){ ... } 欲将其添加到一个ugui toggle ...
- 【剑道】步法(Ashi Sabaki)
转自 http://www.openkendo.com/class7.html 步法(Ashi Sabaki)可能算是剑道中最重要的部分.,以下大致做一归纳讲解,希望能够帮助到各位新人的练习. “折足 ...
- Linux 常见紧急情况处理方法
使用急救盘组进行维护 急救盘组(也称为 boot/root 盘组),是系统管理员必不可少的工具.用它可以独立地启动和运行一个完整的 Linux 系统.实际上,急救盘组中的第 2 张盘上就有一个完整的 ...
- C++哪些运算符重载能够重载?
运算符重载是C++极为重要的语言特性之中的一个.本文将用代码实例回答--C++哪些运算符能够重载?怎样重载?实现运算符重载时须要注意哪些? 哪些运算符能够重载,哪些不可重载? C++98,C++0x, ...
- [AWS vs Azure] 云计算里AWS和Azure的探究(1)
转自:http://www.cnblogs.com/hotcan/archive/2013/01/31/2886794.html 云计算里AWS和Azure的探究(1) 全球领先的云的计算平台主要有两 ...
- [Windows Azure] Configuring and Deploying the Windows Azure Email Service application - 2 of 5
Configuring and Deploying the Windows Azure Email Service application - 2 of 5 This is the second tu ...
- iframe高度自适应方法大全
http://www.3tii.com/993.html http://caibaojian.com/iframe-adjust-content-height.html http://www.jb51 ...
- 线上服务CPU100%问题快速定位实战
功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58速运技术部联合进行了一次线上 ...