java 中英文字符 字节数
英文字母:A
字节数:1; 编码:GB2312
字节数:1; 编码:GBK
字节数:1; 编码:GB18030
字节数:1; 编码:ISO-8859-1
字节数:1; 编码:UTF-8
字节数:4; 编码:UTF-16
字节数:2; 编码:UTF-16BE
字节数:2; 编码:UTF-16LE
中文汉字:人
字节数:2; 编码:GB2312
字节数:2; 编码:GBK
字节数:2; 编码:GB18030
字节数:1; 编码:ISO-8859-1
字节数:3; 编码:UTF-8
字节数:4; 编码:UTF-16
字节数:2; 编码:UTF-16BE
字节数:2; 编码:UTF-16LE
java中如果判断字符 可用 getBytes("GBK").length 来判断
1 public class CutString { 2 3 /** 4 * 判断是否是一个中文汉字 5 * 6 * @param c 7 * 字符 8 * @return true表示是中文汉字,false表示是英文字母 9 * @throws UnsupportedEncodingException 10 * 使用了JAVA不支持的编码格式 11 */ 12 public static boolean isChineseChar(char c) 13 throws UnsupportedEncodingException { 14 // 如果字节数大于1,是汉字 15 // 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了 16 return String.valueOf(c).getBytes("GBK").length > 1; 17 } 18 19 /** 20 * 按字节截取字符串 21 * 22 * @param orignal 23 * 原始字符串 24 * @param count 25 * 截取位数 26 * @return 截取后的字符串 27 * @throws UnsupportedEncodingException 28 * 使用了JAVA不支持的编码格式 29 */ 30 public static String substring(String orignal, int count) 31 throws UnsupportedEncodingException { 32 // 原始字符不为null,也不是空字符串 33 if (orignal != null && !"".equals(orignal)) { 34 // 将原始字符串转换为GBK编码格式 35 orignal = new String(orignal.getBytes(), "GBK"); 36 // 要截取的字节数大于0,且小于原始字符串的字节数 37 if (count > 0 && count < orignal.getBytes("GBK").length) { 38 StringBuffer buff = new StringBuffer(); 39 char c; 40 for (int i = 0; i < count; i++) { 41 c = orignal.charAt(i); 42 buff.append(c); 43 if (CutString.isChineseChar(c)) { 44 // 遇到中文汉字,截取字节总数减1 45 --count; 46 } 47 } 48 return buff.toString(); 49 } 50 } 51 return orignal; 52 } 53 54 public static void main(String[] args) { 55 // 原始字符串 56 String s = "我ZWR爱JAVA"; 57 System.out.println("原始字符串:" + s); 58 try { 59 System.out.println("截取前1位:" + CutString.substring(s, 1)); 60 System.out.println("截取前2位:" + CutString.substring(s, 2)); 61 System.out.println("截取前4位:" + CutString.substring(s, 4)); 62 System.out.println("截取前6位:" + CutString.substring(s, 6)); 63 } catch (UnsupportedEncodingException e) { 64 e.printStackTrace(); 65 } 66 } 67 }
引用: http://blog.csdn.net/yangyan19870319/article/details/6144131

浙公网安备 33010602011771号