标签:des style class blog c code
现在网上大多数用于判断中文字符的是 U+4E00..U+9FA5 这个范围是只是“中日韩统一表意文字”这个区间,但这不是全部,如果要全部包含,则还要他们的扩展集、部首、象形字、注间字母等等;所以,一般用4E00-9FA5已经可以,如果要更广,则用2E80-A4CF || F900-FAFF || FE30-FE4F
package javaTest; import java.util.Random; public class Test { private static int chineseStart = Integer.parseInt(String.valueOf(0x4e00)); private static int chineseEnd = Integer.parseInt(String.valueOf(0x9FA5)); /** * unicode * 汉字区间:4E00-9FA5 * 注音符号:3100-312F * 注音符号(闽南语、客家语扩展):31A0-31BF */ private static String randomChinese(){ Random random = new Random(); int position = random.nextInt(chineseEnd-chineseStart)+chineseStart; System.out.println(position+"--"+chineseEnd); String code = Integer.toHexString(position); return decode("\\u"+code); } /** * @return 所有的中文 */ public String getAllChinese(){ StringBuilder sb = new StringBuilder(); for(int i=chineseStart;i<=chineseEnd;i++){ String ss = Integer.toHexString(i); sb.append(decode("\\u"+ss)); } return sb.toString() ; } /** * 判断是否为中文 * @return */ public static boolean isChinese(String string){ boolean flag = false; for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); if ((c >= 0x4e00) && (c <= 0x9FA5)) { flag = true; } } return flag; } public static void main(String[] args) throws InterruptedException { System.out.println(randomChinese()); System.out.println(isChinese("哈")); } /** * 将unicode编码转成中文字符 * @param unicodeStr * @return */ public static String decode(String unicodeStr) { if (unicodeStr == null) { return null; } StringBuffer sb = new StringBuffer(); int maxLoop = unicodeStr.length(); for (int i = 0; i < maxLoop; i++) { if (unicodeStr.charAt(i) == '\\') { if ((i < maxLoop - 5) && ((unicodeStr.charAt(i + 1) == 'u') || ( unicodeStr.charAt(i + 1) == 'U'))) try { sb.append((char) Integer.parseInt(unicodeStr.substring(i + 2, i + 6), 16)); i += 5; } catch (NumberFormatException localNumberFormatException) { sb.append(unicodeStr.charAt(i)); } else sb.append(unicodeStr.charAt(i)); } else { sb.append(unicodeStr.charAt(i)); } } return sb.toString(); } }
java 随机生成一个中文、判断某个string是否是中文以及打印出全部的中文,布布扣,bubuko.com
java 随机生成一个中文、判断某个string是否是中文以及打印出全部的中文
标签:des style class blog c code
原文地址:http://blog.csdn.net/andywuchuanlong/article/details/26957951