用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数?

如题所述

public class StringTest {
public static void main(String[] args) {
//定义一个测试字符串
String testStr = "this is a test string";

//将该字符串按空格分解成单词数组
String[] words = testStr.split(" ");

//输出单词个数
System.out.println("该字符串一共有" + words.length + "个单词!");

//输出每一个单词
for (int i = 0; i < words.length; i++) {
System.out.println("该字符串的第" + (i+1) + "个单词是:" + words[i]);
}
}
}
温馨提示:答案为网友推荐,仅供参考