怎样判断输入的字符串是否为汉字?
题目提示:输入的字符转化为byte[]类型再判断。
请问该怎样转换,判断。

解决方案 »

  1.   

    You should understand what kind of codes the Chinese words are
    using first, maybe GBcode or UNIcode.for GBcode, if the first byte value >= 0x80, then it's a Chinese word.
    for Unicode, if the first byte value >= 0x80, then it's a Chinese word.of course, there're many other encoding schema for Chinese words,
    e.g. GBK, UTF-8 and etc..
      

  2.   

    String str = new String("..."); str.length();
    new String(str.getBytes(),"8859_1").length();
      

  3.   

    haha, i am coming 2.the up one is quite a answer!so nice, and 0x80 is the kernal ,huh?you have to notice what's the damn ambit of every Chinese character.my regards.
      

  4.   

    String s = "我是谁";
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
    Matcher m = p.matcher(s);
    if(m.find()){
     return m.group.equals(s)
    }
      

  5.   

    楼上的只是包含汉字,
    如果字符串是String s = "abc我是谁";
    也一样匹配
      

  6.   

    如果   believefym(暮色,miss,迷失,miss)   的方法可以检测包含汉字的话就好办了,应该可以变通出全检的,那么谢谢believefym(暮色,miss,迷失,miss) ,建议楼主把分给这位兄弟
      

  7.   

    顺便解释一下这段编码:Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");//[\u4e00-\u9fa5]+这一块我查了一下JDK,太多匹配,没有过这类经验,想听阁下详细帮解释下,楼下兄弟有知者也请尽言,3Q
      

  8.   

    个人感觉,如果追求效率的话,可以不用正则String testStr;for (int i = testStr.length(); i-- > 0; ) {
      char c = testStr.charAt(0);
      if (c >= 0x4e00 && c <= 0x9fa5) {
        return true; //包含就OK
      }
    /*
      if (c < 0x4e00 || c > 0x9fa5) {
        return false; //全部是汉字
      }
    */
    }
      

  9.   

    Pattern p = Pattern.compile("^[\u4e00-\u9fa5]+$");