首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java判断字符串是不是为汉字

2013-08-16 
java判断字符串是否为汉字? ? ?//正则表达式实现? ? ? ?String comm_nameinDto.getAsString(comm_name)

java判断字符串是否为汉字

? ? ?//正则表达式实现

? ? ? ?String comm_name=inDto.getAsString("comm_name");

? ? ? ?Pattern pattern=Pattern.compile("[\\u4e00-\\u9fa5]+"); ?

? ? ? ?Matcher matcher=pattern.matcher(comm_name); ?

? ? ? ?System.out.println(matcher.matches());?

? ? ? ? ? ? ? ? if(matcher.matches()){

? ? ? ?inDto.put("comm_name", inDto.getAsString("comm_name"));

? ? ? ?}else {

? ? ? ?inDto.put("comm_commjm",inDto.getAsString("comm_name").toUpperCase());

? ? ? ?inDto.remove("comm_name");

?

?

?

?

//方法实现

public static boolean isChinese(String str){

?

? ?char[] chars=str.toCharArray();?

? ?boolean isGB2312=false;?

? ?for(int i=0;i<chars.length;i++){

? ? ? ? ? ? ? ?byte[] bytes=(""+chars[i]).getBytes();?

? ? ? ? ? ? ? ?if(bytes.length==2){?

? ? ? ? ? ? ? ? ? ? ? ? ? ?int[] ints=new int[2];?

? ? ? ? ? ? ? ? ? ? ? ? ? ?ints[0]=bytes[0]& 0xff;?

? ? ? ? ? ? ? ? ? ? ? ? ? ?ints[1]=bytes[1]& 0xff;?

? ? ? ? ? ? ? ? ? ? ? ? ? ?if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?isGB2312=true;?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;?

? ? ? ? ? ? ? ? ? ? ? ? ? ?}?

? ? ? ? ? ? ? ?}?

? ?}?

? ?return isGB2312;?

}

?

热点排行