请你用java写一个方法,计算字符串“KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world”的单词个数。

解决方案 »

  1.   

    String textString = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";String tempString = textString.replaceAll(" ", "");int wordNum = (textString.length - tempString.length) + 1;
      

  2.   


    String str = "KWA, where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";
         System.out.println(str.split("[, ]+").length);
      

  3.   


    String str = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";        String []a=str.split(" |,");
            for(String sss:a)
            {
                System.out.println(sss);
            }
            System.out.println(a.length);注意全半角标点
      

  4.   

    I assume that "world's" is one word. And also we know there is no "," in English, but "," does exist.
      

  5.   


    我想,这个应该是最佳答案了,其实面试题就是考正则表达式的吧?btw:,应该是,全角半角的区别,不过都是小问题了。
      

  6.   

    maybe I need to replaceAll("[, ]", "");
      

  7.   


    String s = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";
    String[] str=(s.split("[\\,| ]"));
    System.out.println(str.length);主要是半角还是全角的问题 ....
      

  8.   

    public static void main(String[] args) {
    String date = (new Date(System.currentTimeMillis())).toString();
    String abc="“KWA,fd,fsd,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world”";
    String []abcd=abc.split(" |,|,");
    int i=0;
    for(String s:abcd){
    System.out.println("第["+(++i)+"]个单词是:"+s);
    System.out.println(++i);
    }
    System.out.println("总共:"+i+"个单词.");
    }
    =================================================
    这段代码是仅针对你这段作的统计单词的个数.