java字元操作獲取漢字的拼音以及其它經常使用工具

2021-09-07 05:40:50 字數 4346 閱讀 1640

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

忽然加了個需求,說要實現漢字轉拼音查詢。

在努力下寫出來了,如今分享一下吧!。!

/** * 漢字轉拼音縮寫 * * @param str * 要轉換的漢字字串 * @return string 拼音縮寫 */ public static string getpystring(string str) else } if (tempstr.length() > 1) return tempstr; } /** * 取單個字元的拼音聲母 * * @param c * //要轉換的單個漢字 * @return string 拼音聲母 */ public static string getpychar(string c)

另外在做程式的時候,非常多時候都是須要做時間字元轉譯。填充下

文章的內容吧。如今給出相應的時間字元的轉換。!。

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) // 返回相應的時間字串 public static string getstrbydate2(date date) // 返回相應的時間字串 public static string getstrbydate1(date date) /** * 將日期轉換成字串格式 * * @param date * @return */ public static string datetran2str(date date) /** * 將日期轉換成字串格式 * * @param date * @return */ public static string billrandom(date date) /** * 將日期轉換成字串格式 * * @param date * @param format日期格式 * @return */ public static string datetran2str(date date, string format) /** * 將日期轉換成字串格式 * * @param date * @param format日期格式 * @return */ public static string datetran2str(object date, string format) /** * 將日期轉換成字串格式 * * @param date * @param format日期格式 * @return */ public static string datetran2str(object date) /** * 帶秒的時間格式 * * @param date * @param format日期格式 * @return */ public static string datetranstr2exc(object date) /** * 將日期型字串轉換為日期 * * @param datestr * 一定要是日期轉過來的日期型字串,否則會有parseexception * @return null表示parseexception */ public static date strtran2date(string datestr) catch (parseexception e) return null; } /** * 將日期型字串轉換為日期 * * @param datestr * 一定要是日期轉過來的日期型字串。否則會有parseexception * @param pattern指定的時間格式 * @return null表示parseexception */ public static date strtran2date(string datestr, string pattern) catch (parseexception e) return null; } /** * float型資料轉換成string * * @param f * @return */ public static string floattran2str(float f) /** * 將時間轉換成long型時間戳 * * @param timestr * 時間的字串表示,格式為:hh:mm * @return */ public static long strtran2time(string timestr) time = timeformat.parse(timestr); return time.gettime(); } catch (parseexception e) return null; } /** * 將資料庫的時間轉換成字串 * * @param t時間 * @return */ public static string timetran2str(object t) /** * 將時間戳轉換成字串 * * @param timestampstring * @return */ public static string timestamp2date(string timestampstring) public static string strtranbirth(string day) return day; }

最後,我們常常要做一些字元為空推斷之類的,和其它資料的正則

表示式的驗證,如今也給出福利吧。。。

/** * 推斷輸入的字串引數是否為空 * * @return boolean 空則返回true,非空則flase */ public static boolean isempty(string input) /** * 推斷輸入的位元組陣列是否為空 * * @return boolean 空則返回true,非空則flase */ public static boolean isempty(byte bytes) /** * 位元組陣列轉為字串 * * @see 該方法預設以iso-8859-1轉碼 * @see 若想自己指定字符集,能夠使用getstring(byte data, string charset)方法 */ public static string getstring(byte data) catch (exception e) return str; } // 匹配輸入資料型別 public static boolean matchcheck(string ins, int type) $"; 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]$"; break; case 3: // /password pat = "^[\\s\\s]$"; break; case 4: // /中文 pat = "^[0-9a-z\u4e00-\u9fa5|admin]$"; 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$|^\\d$|^\\d(\\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獲取漢字拼音首字母

在專案中要更能根據某些查詢條件 比如姓名 的首字母作為條件進行查詢,比如查乙個叫 李曉明 的人,可以輸入 lxm 寫了乙個工具類如下 取得給定漢字串的首字母串,即聲母串 title chinesechartoen date 2004 02 19 注 只支援gb2312字符集中的漢字 public f...

java 獲取漢字拼音的首字母

在專案中要更能根據某些查詢條件 比如姓名 的首字母作為條件進行查詢,比如查乙個叫 張三 的人,可以輸入 zs 寫了乙個工具類如下 gb 2312 80 把收錄的漢字分成兩級。第一級漢字是常用漢字,計 3755 個,置於 16 55 區,按漢語拼音字母 筆形順序排列 第二級漢字是次常用漢字,計 300...

java 獲取漢字拼音的首字母

在專案中要更能根據某些查詢條件 比如姓名 的首字母作為條件進行查詢,比如查乙個叫 張三 的人,可以輸入 zs 寫了乙個工具類如下 gb 2312 80 把收錄的漢字分成兩級。第一級漢字是常用漢字,計 3755 個,置於 16 55 區,按漢語拼音字母 筆形順序排列 第二級漢字是次常用漢字,計 300...