判断接收的字符串是英文还是中文

来源:互联网 发布:数据字典表设计 编辑:程序博客网 时间:2024/06/13 07:20

这只是其中一种方法,通过比较存储的空间大小判断

public class ChineseOrEnglishUtil{  public static String isChineseOrEnglish(String charaString){      boolean result =false;      //英文      result=charaString.matches("^[a-zA-Z]*");      if(result){          return "isEnglish";      }      //中文      String regEx = "[\\u4e00-\\u9fa5]+";      Pattern p = Pattern.compile(regEx);      Matcher m = p.matcher(charaString);      result=m.find();     if(result){         return "isChinese";     }else{         return "isChineseAndEnglish";//中英结合     }   }  public static String checkChineseOrEnglish(String word){      byte []bytes = word.getBytes();        if(bytes.length==word.length()){          return "isEnglish";      }else{          return "isChinese";      }  }    public static void main(String[] args) {        System.out.println(ChineseOrEnglishUtil.isChineseOrEnglish("明"));        System.out.println(ChineseOrEnglishUtil.checkChineseOrEnglish("ming"));        System.out.println(ChineseOrEnglishUtil.checkChineseOrEnglish("明"));    }}
原创粉丝点击