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"*" ...
随机推荐
- 【原】spring boot在整合项目依赖的问题
最近要开发新的项目,就花了几天时间看了下spring boot的相关资料,然后做了一个demo,不得不说开发效率确实很快,几行注解就完成了事务,aop,数据库等相关配置:但由于先前习惯了spring ...
- ARM简介(科普文)
ARM简介[1] 1. ARM只卖知识产权,不卖(物理的,实质的)产品. 全世界100多家公司购买了ARM授权,包括三星,Freescale.NXP Semiconductors.STMicro ...
- jquery选择器控制Html元素
1.JQuery中有addClass,removeClass,toggleClass addClass(class):为每个匹配的元素添加指定的类名 removeClass(class):从所有匹配的 ...
- Sublime Text 2使用技巧汇总
一.下载链接: Windows-64bit: http://pan.baidu.com/s/1o6QdKYu 其它版本请移步官网: http://www.sublimetext.com/ 二.破解Li ...
- 海量Web日志分析 用Hadoop提取KPI统计指标
http://blog.fens.me/hadoop-mapreduce-log-kpi/ http://dongxicheng.org/search-engine/scribe-installati ...
- 1298 The Hardest Problem Ever
题目链接:http://poj.org/problem?id=1298 思路分析:水题,字符偏移求解,注意字符串输入问题即可. 代码如下: #include <iostream> #inc ...
- 西门子PLC学习笔记二-(工作记录)
今天师傅给讲了讲做自己主动化控制的总体的思路,特进行一下记录,做个备忘. 1.需求分析 本次的项目是对楼宇循环供水的控制,整个项目须要完毕压力.压差.温度等的获取及显示.同一时候完毕电机的控制. 2. ...
- BestCoder Round #50 (div.1) 1001 Distribution money (HDU OJ 5364)
题目:Click here 题意:bestcoder上面有中文题目 #include <iostream> #include <cstdio> #include <cst ...
- javascript 数组部分
<html> <body> <script type="text/javascript"> var arr = new Array(6) arr ...
- Android ListView两种长按弹出菜单方式
转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...