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]);
}
}
}