String s = "我的的01天啊2345我的啊的678901天23456789啊1";
  Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(s.substring(0, 30));
  int count = 0;
  while (matcher.find()) {
  count++;
                     System.out.println(" 所在位置:");    
  }
  System.out.println(" 汉字个数:"+count);
谢谢大侠啊

解决方案 »

  1.   


    public static void main(String[] args) {
    String s = "我的的01天啊2345我的啊的678901天23456789啊1";
    int count = 0;
    for(int i = 0; i < s.length(); i++){
    Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(s.substring(i, i + 1));
    if(matcher.matches()){
    System.out.println(" 所在位置:" + i);
    count++;
    }else{
    System.out.println(i + " 位置上不是汉字");
    }
    }
    System.out.println(" 汉字个数:"+count);
    }这样吧,用find的话会重新排序的,得不到原来的位置
      

  2.   


                    while (matcher.find()) {
    count++;
    System.out.println(" 所在位置:" + matcher.start());
    }
      

  3.   


    加上matcher.start()方法就可得到当前字符所在下标。
      

  4.   

    另外,第2行语句有问题,所以找出来的汉字少了一个:
    Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(s);
      

  5.   

    偷懒的做法
    String s = "我的的01天啊2345我的啊的678901天23456789啊1";
    System.out.printf("汉字个数:%s\n", s.getBytes().length - s.length());
    for (int i=0; i<s.length(); i++) {
        String sub = s.substring(i, i+1);
        if (sub.getBytes().length > sub.length()) {
            System.out.printf("汉字:%s, 位置:%d\n", sub, i);
        }
    }