当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java判断中文
发布时间:2010/9/26 11:45:43 来源:城市学习网 编辑:ziteng
  1、String str1 = "字符串aa";
  for(int i=0;i<str1.length();i++)
  {
  String test=str1.substring(i,i+1);
  System.out.println(test);
  if(test.matches("[\\u4E00-\\u9FA5]+"))
  {
  System.out.println("中文");
  }
  }
  2、System.out.println(str1.length()==str1.getBytes().length?”English”:”Contains Chinese”);
  String str="我爱你-呵呵";
  if(str.matches("[\\u4E00-\\u9FA5]+"))     //false
  System.out.println("包含中文......"); //匹配整个字符串
  Pattern p=Pattern.compile("[\\u4E00-\\u9FA5]+");
  Matcher m=p.matcher(str);
  while(m.find()){                          //局部匹配
  System.out.println("包含中文***********"+m.group(0));
  //m.group(0) 表示匹配的中文字符(我爱你,呵呵)
  }
  String charType=str.length()==str.getBytes().length?"English":"中文";
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved