package com.youku.paycenter.admin.subGate.service.impl;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestString
{
public static final String CHINESE_REG_EXP = "[\u4e00-\u9fa5]";public static void main(String[] args) throws UnsupportedEncodingException{ String s = "故人西辭黃鶴樓,煙花三月下揚州;孤帆遠影碧空盡,唯見長江天際流。故人西辭黃鶴樓,煙花三月下揚州;孤帆遠影碧空盡,唯見長江天際流。故人西辭黃鶴樓,煙花三月下揚州;孤帆遠影碧空盡,唯見長江天際流。故人西辭黃鶴樓,煙花三月下揚州;孤帆遠影碧空盡,唯見長江天際流。故人西辭黃鶴樓,煙花三月下揚州;孤帆遠影碧空盡,唯見長江天際流。故人西辭黃鶴樓,煙花三月下揚州;孤帆遠影碧空盡,唯見長江天際流。故人西辭黃鶴樓,abc"; if(isChineseChar(s)) { char[] c_array = s.toCharArray(); String c_string = null; byte[] c_byte_array; int chineseCount = 0; int normalCount = 0; for (char c : c_array) { c_string = Character.toString(c); c_byte_array = c_string.getBytes("UTF-8"); if(c_byte_array.length>1) { chineseCount++; } else { normalCount++; } } System.out.println(chineseCount+normalCount); }}public static boolean isChineseChar(String str){ if (str == null) return false; boolean temp = false; Pattern p = Pattern.compile(CHINESE_REG_EXP); Matcher m = p.matcher(str); if (m.find()) { temp = true; } return temp;}
}