获取汉字的加1位的汉字,可以用于sql语句查询的过程中,用大于或小于号来代替sql的like查询如"xxx%" 这种形式,可以更好的加快sql语句的查询速度,这个文章估计没有人看,小编只是作为自己的笔记存储起来,以备不时之需。
package com.xuanyin.homektv.util; import java.util.regex.Pattern; public final class UicodeBackslashU { private static final String singlePattern = "[0-9|a-f|A-F]"; private static final String pattern = singlePattern + singlePattern + singlePattern + singlePattern; private static String ustartToCn(final String str) { StringBuilder sb = new StringBuilder().append("0x") .append(str.substring(2, 6)); Integer codeInteger = Integer.decode(sb.toString()); int code = codeInteger.intValue(); char c = (char)code; return String.valueOf(c); } private static boolean isStartWithUnicode(final String str) { if (null == str || str.length() == 0) { return false; } if (!str.startsWith("\\u")) { return false; } if (str.length() < 6) { return false; } String content = str.substring(2, 6); boolean isMatch = Pattern.matches(pattern, content); return isMatch; } public static String unicodeToCn(final String str) { StringBuilder sb = new StringBuilder(); int length = str.length(); for (int i = 0; i < length;) { String tmpStr = str.substring(i); if (isStartWithUnicode(tmpStr)) { sb.append(ustartToCn(tmpStr)); i += 6; } else { sb.append(str.substring(i, i + 1)); i++; } } return sb.toString(); } //返回中文最后一位的unicode加一的中文 public static String cn2cn(String str){ String str1 = str.substring(str.length()-1); String str2 = str.substring(0,str.length()-1); char chars = str1.charAt(0); char a = 1; String str3 = "\\u" + Integer.toString(chars+a, 16); return str2+unicodeToCn(str3); } }
可以这样使用上方代码,如下:
String s = UnicodeNextWordUtils.getNextWord("我"); System.out.println(s);
输出的汉字unicode加一的结果为:戒