java字符操作获取汉字的拼音以及其它经常使用工具
公司需求年年有,今年有点小特殊,哈哈。
忽然加了个需求,说要实现汉字转拼音查询。
在努力下写出来了,如今分享一下吧!。!
/**
* 汉字转拼音缩写
*
* @param str
* 要转换的汉字字符串
* @return String 拼音缩写
*/
public static String getPYString(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));
}
}
if (tempStr.length() > 1) {
tempStr = tempStr.substring(0, 1);
}
return tempStr;
}
/**
* 取单个字符的拼音声母
*
* @param c
* //要转换的单个汉字
* @return String 拼音声母
*/
public 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 "*";
}
</textarea >
另外在做程序的时候,非常多时候都是须要做时间字符转译。填充下
文章的内容吧。如今给出相应的时间字符的转换。!。
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";
public static final String DATE_FORMAT2 = "yyyy-MM-dd HH:mm:ss";
public static final String YEAR_MONTH_FORMAT = "yyyy-MM";
public static final String DATE_FORMAT3 = "HH:mm:ss";
public static final String DATE_FORMAT1 = "yyyyMMddHHmmss";
/**
* 时间格式,採用24小时制
*/
public static final String TIME_FORMAT = "HH:mm";
/**
* 不带秒的时间日期格式
*/
public static final String DATE_FORMAT_NO_SECOND = "yyyy-MM-dd HH:mm";
/**
* 不是时间的日期格式
*/
public static final String DATE_FORMAT_NO_TIME = "yyyy-MM-dd";
// 返回相应的时间字符串
public static String getStrByDate(Date date) {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
str = sdf.format(date);
return str;
}
// 返回相应的时间字符串
public static String getStrByDate2(Date date) {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
str = sdf.format(date);
return str;
}
// 返回相应的时间字符串
public static String getStrByDate1(Date date) {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2);
str = sdf.format(date);
return str;
}
/**
* 将日期转换成字符串格式
*
* @param date
* @return
*/
public static String dateTran2Str(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
StrUtil.DATE_FORMAT_NO_SECOND);
return dateFormat.format(date);
}
/**
* 将日期转换成字符串格式
*
* @param date
* @return
*/
public static String BillRandom(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(StrUtil.DATE_FORMAT1);
return dateFormat.format(date);
}
/**
* 将日期转换成字符串格式
*
* @param date
* @param format日期格式
* @return
*/
public static String dateTran2Str(Date date, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
/**
* 将日期转换成字符串格式
*
* @param date
* @param format日期格式
* @return
*/
public static String dateTran2Str(Object date, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
/**
* 将日期转换成字符串格式
*
* @param date
* @param format日期格式
* @return
*/
public static String dateTran2Str(Object date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
DATE_FORMAT_NO_SECOND);
return dateFormat.format(date);
}
/**
* 带秒的时间格式
*
* @param date
* @param format日期格式
* @return
*/
public static String dateTranStr2exc(Object date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT2);
return dateFormat.format(date);
}
/**
* 将日期型字符串转换为日期
*
* @param dateStr
* 一定要是日期转过来的日期型字符串,否则会有ParseException
* @return null表示ParseException
*/
public static Date strTran2Date(String dateStr) {
try {
return new SimpleDateFormat(StrUtil.DATE_FORMAT_NO_SECOND)
.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 将日期型字符串转换为日期
*
* @param dateStr
* 一定要是日期转过来的日期型字符串。否则会有ParseException
* @param pattern指定的时间格式
* @return null表示ParseException
*/
public static Date strTran2Date(String dateStr, String pattern) {
try {
return new SimpleDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* float型数据转换成String
*
* @param f
* @return
*/
public static String floatTran2Str(float f) {
return String.valueOf(f);
}
/**
* 将时间转换成long型时间戳
*
* @param timeStr
* 时间的字符串表示,格式为:HH:mm
* @return
*/
public static Long strTran2Time(String timeStr) {
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
Date time = null;
try {
int len = timeStr.length();
if (timeStr.length() > 10) {
timeStr = timeStr.substring(10);
}
time = timeFormat.parse(timeStr);
return time.getTime();
} catch (ParseException e) {
System.out.println(new Exception("日期格式非法!
").getMessage());
}
return null;
}
/**
* 将数据库的时间转换成字符串
*
* @param t时间
* @return
*/
public static String timeTran2Str(Object t) {
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
String timeStr = null;
timeStr = timeFormat.format(t);
return timeStr;
}
/**
* 将时间戳转换成字符串
*
* @param timestampString
* @return
*/
public static String timeStamp2Date(String timestampString) {
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new java.text.SimpleDateFormat("yyyy-MM-dd")
.format(new java.util.Date(timestamp));
return date;
}
public static String strTranBirth(String day) {
if (day.length() == 8) {
String y = day.substring(0, 4);
String m = day.substring(4, 6);
String d = day.substring(6, 8);
return y + "-" + m + "-" + d;
}
return day;
}
最后,我们常常要做一些字符为空推断之类的,和其它数据的正则
表达式的验证,如今也给出福利吧。。。
/**
* 推断输入的字符串參数是否为空
*
* @return boolean 空则返回true,非空则flase
*/
public static boolean isEmpty(String input) {
return null == input || 0 == input.length()
|| 0 == input.replaceAll("\\s", "").length();
}
/**
* 推断输入的字节数组是否为空
*
* @return boolean 空则返回true,非空则flase
*/
public static boolean isEmpty(byte[] bytes) {
return null == bytes || 0 == bytes.length;
}
/**
* 字节数组转为字符串
*
* @see 该方法默认以ISO-8859-1转码
* @see 若想自己指定字符集,能够使用<code>getString(byte[] data, String charset)</code>方法
*/
public static String getString(byte[] data) {
String str = "";
try {
str = new String(data, "ISO-8859-1");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
// 匹配输入数据类型
public static boolean matchCheck(String ins, int type) {
String pat = "";
switch (type) {
case 0: // /手机号
pat = "^1[3-8][0-9]{9}$";
break;
case 1:// /邮箱
pat = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
break;
case 2: // /username
pat = "^[0-9a-zA-Z]{4,12}$";
break;
case 3: // /password
pat = "^[\\s\\S]{6,18}$";
break;
case 4: // /中文
pat = "^[0-9a-z\u4e00-\u9fa5|admin]{2,15}$";
break;
case 5: // /非零正整数
pat = "^\\+?[1-9][0-9]*$";
break;
case 6: // /数字和字母
pat = "^[A-Za-z0-9]+$";
break;
case 7: // /1-9的数字
pat = "^[1-9]";
break;
case 8: // /身份证
pat = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
break;
case 9: // /名字
pat = "^([A-Za-z]|[\u4E00-\u9FA5])+$";
break;
case 10: // /时间 时:分:秒
pat = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
break;
}
Pattern p = Pattern.compile(pat);
Matcher m = p.matcher(ins);
return m.matches();
}
java字符操作获取汉字的拼音以及其它经常使用工具的更多相关文章
- iOS获取汉字的拼音
在iOS开发中经常涉及到汉字的排序,最常见的就是需要根据首字母的字符顺序排列,比如常见的通讯录等.总结出来,大致可以分为两种方法,其中参考文献[1]中提供的方法十分复杂,利用查表的方法是先,并且代码量 ...
- C# 获取汉字的拼音首字母和全拼(含源码)
C# 获取汉字的拼音首字母 一种是把所有中文字符集合起来组成一个对照表:另一种是依照汉字在Unicode编码表中的排序来确定拼音的首字母.碰到多音字时就以常用的为准(第一种方法中可以自行更改,方法为手 ...
- php获取汉字的拼音 拼音首字母
/***获取汉字的拼音*/function pinyin($s, $isfirst = false) { static $pinyins; $s = trim($s); $len = strlen($ ...
- (转载)delphi中获取汉字的拼音首字母
delphi中获取汉字的拼音首字母1.py: array[216..247] of string = ({216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + ' ...
- sql获取汉字的拼音首字母的函数
ql获取汉字的拼音首字母 if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...
- Java通过pinyin4j实现汉字转拼音
碰到个需求,需要按用户名字的首字母来排序.这就需要获取汉字对应的拼音了,突然就想起了pinyin4j这个jar包,于是就开始写了个汉字转拼音的工具类.在此记录一下,方便后续查阅 一.Pom依赖 ...
- 获取汉字的拼音首字母--pinyin
var pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, options = { checkPo ...
- C# 获取汉字的拼音首字母
/// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...
- C# 获取汉字转拼音缩写-简写,不是全拼
///<summary> /// 汉字转拼音缩写 /// Code By ] -'\0')); if ( i <0xB0A1) return"*" ...
随机推荐
- Laravel 简单使用七牛云服务
前言 路漫漫其修远兮,吾将上下而求索.学习 Laravel 之初觉得所有东西都很厉害的样子,现在看来就是很厉害啊!最近在写一个项目上传的模块,要上传图片到七牛云,昨天看了一下午七牛云官方的文档感觉还是 ...
- openrisc 之 Wishbone总线学习笔记——总线互联
一,总线命名规范 1,wishbone总线接口信号都是高电平有限 2,wishbone接口信号都是以 _i ,或者是 _o 结束.i表示输入, o表示输出. ()表示该信号为总线信号,总线位宽可以大于 ...
- AutoCAD 2013官方简体中文破解版(32 / 64位),带激活码和注册机
AutoCAD 2014下载地址:http://ideapad.zol.com.cn/61/160_603697.html 安装及破解方法:(注册机下载在下方) 1.安装Autodesk AutoCA ...
- 条码知识之十:EAN-128条码(下)
国际物品编码协会(EAN)和美国统一代码委员会(UCC)将CODE-128码引入EAN/UCC系统,并作如下规定:起始符由一个START A/B/C 加一个辅助字符FNC1构成,以区别普通的CODE- ...
- Qt Creator插件工作流程代码走读
Qt Creator有个很风骚的插件管理器PluginManager,还有个很骚包的插件说明PluginSpec.基本上,所有的Qt程序的入口都是传统的C程序一样,代码流程从main()函数开始. ...
- 基于QtQuick2.0应用程序运行于XP系统的诸多问题
客户端 使用QtQuick技术开发酷炫的XP客户端经常遇到白屏或者无界面 if Qt is built using ANGLE, its shared libraries and the requir ...
- logback自定义格式转换器
创建自定义格式转换符有两步. 首先,必须继承ClassicConverter类.ClassicConverter对象负责从ILoggingEvent 提取信息,并产生一个字符串.例如,LoggerCo ...
- cocos2d-x游戏开发系列教程-超级玛丽05-CMMenuScene
代码下载链接 http://download.csdn.net/detail/yincheng01/6864893 解压密码:c.itcast.cn 背景 上一篇博文提到appDelegate,在该类 ...
- 在 Azure 网站上使用 Memcached 改进 WordPress
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. 您是否希望改善在 ...
- POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..
dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t) < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...