公司需求年年有,今年有点小特殊,哈哈。

忽然加了个需求,说要实现汉字转拼音查询。

在努力下写出来了,如今分享一下吧!。!

/**
* 汉字转拼音缩写
*
* @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字符操作获取汉字的拼音以及其它经常使用工具的更多相关文章

  1. iOS获取汉字的拼音

    在iOS开发中经常涉及到汉字的排序,最常见的就是需要根据首字母的字符顺序排列,比如常见的通讯录等.总结出来,大致可以分为两种方法,其中参考文献[1]中提供的方法十分复杂,利用查表的方法是先,并且代码量 ...

  2. C# 获取汉字的拼音首字母和全拼(含源码)

    C# 获取汉字的拼音首字母 一种是把所有中文字符集合起来组成一个对照表:另一种是依照汉字在Unicode编码表中的排序来确定拼音的首字母.碰到多音字时就以常用的为准(第一种方法中可以自行更改,方法为手 ...

  3. php获取汉字的拼音 拼音首字母

    /***获取汉字的拼音*/function pinyin($s, $isfirst = false) { static $pinyins; $s = trim($s); $len = strlen($ ...

  4. (转载)delphi中获取汉字的拼音首字母

    delphi中获取汉字的拼音首字母1.py: array[216..247] of string = ({216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + ' ...

  5. sql获取汉字的拼音首字母的函数

    ql获取汉字的拼音首字母   if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...

  6. Java通过pinyin4j实现汉字转拼音

       碰到个需求,需要按用户名字的首字母来排序.这就需要获取汉字对应的拼音了,突然就想起了pinyin4j这个jar包,于是就开始写了个汉字转拼音的工具类.在此记录一下,方便后续查阅 一.Pom依赖 ...

  7. 获取汉字的拼音首字母--pinyin

    var pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, options = { checkPo ...

  8. C# 获取汉字的拼音首字母

    /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...

  9. C# 获取汉字转拼音缩写-简写,不是全拼

    ///<summary> /// 汉字转拼音缩写 /// Code By         ] -'\0')); if ( i <0xB0A1) return"*" ...

随机推荐

  1. centos5.5 安装git

    查看centos版本 # cat /etc/redhat-release CentOS release 5.5 (Final) 安装git 下载: 如果有老版本的git: #git clone git ...

  2. 字符串匹配算法(KMP)

    字符串匹配运用很广泛,举个简单例子,我们每天登QQ时输入账号和密码,大家有没有想过账号和密码是怎样匹配的呢?登录需要多长时间和匹配算法的效率有直接的关系. 首先理解一下前缀和后缀的概念: 给出一个问题 ...

  3. 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】

    写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...

  4. linux 内核分析之list_head

    转自:http://www.cnblogs.com/riky/archive/2006/12/28/606242.html 一.链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指针将一 ...

  5. 转:从pickle看python类成员的动态加载和类的定位

      pickle是Python轻便的对象序列化工具.使用pickle可以方便地把python对象写入文件对象中,或者像soap那样在socket间传送.     按照python的一贯作风,类的成员在 ...

  6. Eclipse 取消import自动补全具体的类名

    有时候,在代码里写了一个JFrame,然后Eclipse就自动添加了import javax.swing.JFrame; 但有时候希望只要import javax.swing.*;就可以了,不希望具体 ...

  7. 简单浮点数除法模拟-hdu-4493-Tutor

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4493 题目意思: 给小数点后两位的12个月的工资,求出平均工资,输出离小数点后第二位最近的两位小数, ...

  8. Android Texting(2)Testing Fundamentals 测试基础篇

    Testing Fundamentals The Android testing framework, an integral part of the development environment, ...

  9. 你会用swift创建复杂的加载动画吗(1)

    时至今日,iOS 应用商店已经拥有超过了140万 应用,让你自己的应用脱颖而出确实是个不小的挑战.不过,在你的应用掉入默默无闻的大黑洞之前,你拥有一个小小的机遇窗,它能帮你吸引用户的注意. AD: 时 ...

  10. nginx源代码学习资源(不断更新)

    nginx源代码学习是一个痛苦又快乐的过程,以下列出了一些nginx的学习资源. 首先要做的当然是下载一份nginx源代码,能够从nginx官方站点下载一份最新的. 看了nginx源代码,发现这是一份 ...