题目
统计一篇文档资料中单词的个数(提示文档资料可放在字符串中,利用正则表达式来实现)
import java.util.Scanner;
public class DemoTest{
String regex="[\\d\\s\\p{Punct}]+";//用正则表达式来作为分割标记来分解出其中的子字符串
public int Count(String str){
String[]arr=str.split(regex);
int sum=0;
for(int i=0;i<arr.length;i++){//并输出各个单词
System.out.println(arr[i]);
}
return arr.length;
}
public static void main(String[] args){
Scanner t=new Scanner(System.in);
String str=t.nextLine();//字符串的输入
DemoTest A=new DemoTest();
System.out.println(A.Count(str));
}
}