请你用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”的单词个数。
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;
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);
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);注意全半角标点
I assume that "world's" is one word. And also we know there is no "," in English, but "," does exist.
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);主要是半角还是全角的问题 ....
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+"个单词."); } ================================================= 这段代码是仅针对你这段作的统计单词的个数.
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);
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);注意全半角标点
我想,这个应该是最佳答案了,其实面试题就是考正则表达式的吧?btw:,应该是,全角半角的区别,不过都是小问题了。
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);主要是半角还是全角的问题 ....
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+"个单词.");
}
=================================================
这段代码是仅针对你这段作的统计单词的个数.